What am I missing here?
public class abc extends JFrame {
private JButton save = new JButton("Save");
public abc() {
JPanel p = new JPanel();
save.addActionListener(new SaveL());
p.add(save);
Container cp = getContentPane();
p = new JPanel();
p.setLayout(new GridLayout(2, 1));
cp.add(p, BorderLayout.NORTH);
}
}
class SaveL implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.out.println("Hello"); // nothing happens
}
}
Why doesn't my ActionListener work here
You are creating a JPanel, adding your JButton to it, then creating a new JPanel and adding that panel to your JFrame. You need to be adding the original panel to your content pane.
Your code is completely messed up. You instantiate your JPanel p twice, your button is declared "open" but is actually "save". You mix GridLayout with BorderLayour constraints. The following code works:
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class abc extends JFrame {
private JButton save = new JButton("Save");
public abc() {
JPanel p = new JPanel();
save.addActionListener(new SaveL());
p.add(save);
p.setLayout(new GridLayout(2, 1));
add(p);
}
public static void main(String[] args) {
abc abc = new abc();
abc.pack();
abc.setVisible(true);
}
}
class SaveL implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("Hello"); // nothing happens
}
}
Your code is re-creating the panel. It's losing the button.
I changed it to:
public class abc extends JFrame{
private JButton save = new JButton("Save");
public abc() {
JPanel p = new JPanel();
save.addActionListener(new SaveL());
p.add(save);
Container cp = getContentPane();
cp.add(p, BorderLayout.NORTH);
}
}
class SaveL implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.out.println("Hello"); // nothing happens
}
}
and it worked
private JButton open = new JButton("Save");
save.addActionListener(new SaveL());
Did u declare your save as open?
Related
ParentFrame shows ArrayList and one "ADD" Button. Once I click "ADD" Button on ParentFrame, then ChildFrame shows up.
On ChildFrame, I type in a String and click "OK" Button then it should transfer its String to ParentFrame. Finally ParentFrame should be repainted with newly added String.
I'm having trouble with repainting but also I might failed to send String from Child to Parent since Parent didn't get repainted.
I tried several things in two or three other points of view but following code seems like to work but......
I need your help!!
ParentFrame
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import java.awt.*;
#SuppressWarnings("serial")
public class parentFrame extends JFrame {
ArrayList<String> list = new ArrayList<>(){{add("test1"); add("test2");}};
JButton add;
JPanel big, small;
JLabel content;
childFrame addFrame;
public parentFrame() {
super("parent frame");
super.setLayout(new BorderLayout());
super.setSize(600,600);
big = new JPanel();
for(int i=0; i<list.size(); i++) {
content = new JLabel();
content.setText(list.get(i));
big.add(content);
}
super.add(big, BorderLayout.CENTER);
add = new JButton("ADD");
add.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
addFrame = new childFrame();
/* By next 3 lines, I'm trying to transfer the value of childFrame's test to this parentFrame's list. */
list.add(addFrame.getTestString());
big.revalidate();
big.repaint();
}
});
super.add(add, BorderLayout.SOUTH);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new parentFrame();
}
}
2.ChildFrame
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
#SuppressWarnings("serial")
public class childFrame extends JFrame {
String test;
JTextField name;
JButton ok, cancel;
public childFrame() {
super("child frame");
super.setLayout(new BorderLayout());
super.setSize(400,200);
JPanel centerPanel = new JPanel(new GridLayout(1,1));
centerPanel.setSize(150, 100);
name = new JTextField();
centerPanel.add(name);
JPanel bottomPanel = new JPanel(new FlowLayout());
ok = new JButton("OK");
ok.addActionListener(new OKListener());
super.add(ok);
cancel = new JButton("CANCEL");
cancel.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
dispose();
}
});
bottomPanel.add(cancel);
bottomPanel.add(ok);
super.add(centerPanel, BorderLayout.CENTER);
super.add(bottomPanel, BorderLayout.SOUTH);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
class OKListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
test = name.getText();
dispose();
}
}
public String getTestString() {
return test;
}
}
Your problem is here:
public void actionPerformed(ActionEvent e) {
addFrame = new childFrame();
list.add(addFrame.getTestString());
big.revalidate();
big.repaint();
}
Since your child frame is not a modal window (for example, a JOptionPane is a modal dialog window), it does not halt program flow in the calling window. You call .getTestString() immediately on creation of the child frame but before the user has had any chance to enter in any data (again, because program flow in the calling window is not halted).
The solution is to make your child "frame" in fact a modal JDialog. This will pretty much solve the whole issue. So, don't have the child frame extend from JFrame, but rather extend JDialog, and use the JDialog constructor that makes it modal (see the JDialog API).
e.g.,
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
#SuppressWarnings("serial")
// note that class names should begin with an upper-case letter
public class ChildFrame extends JDialog {
String test;
JTextField name;
JButton ok, cancel;
public ChildFrame(JFrame parentFrame) {
// the true parameter makes this modal
super(parentFrame, "child frame", true);
Now this dialog window will freeze program flow from the calling code as soon as the dialog is set visible, and the calling code flow won't resume until this dialog is no longer visible.
Also, please have a look at The Use of Multiple JFrames, Good/Bad Practice?
An alternative to this is to continue to use multiple JFrames (not recommended!!), and add a WindowListener to the "child" window, listening for windows closing events, and then getting the information from your dialog in call-back method that is activated when the windows closing event occurs.
For a working example:
import java.awt.BorderLayout;
import java.awt.Window;
import java.awt.Dialog.ModalityType;
import java.awt.event.*;
import javax.swing.*;
#SuppressWarnings("serial")
public class ParentGuiPanel extends JPanel {
private DefaultListModel<String> listModel = new DefaultListModel<>();
private JList<String> jList = new JList<>(listModel);
private JButton addButton = new JButton("Add");
private JDialog childDialog;
private ChildGuiPanel childPanel = new ChildGuiPanel();
public ParentGuiPanel() {
listModel.addElement("Test 1");
listModel.addElement("Test 2");
jList.setPrototypeCellValue("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
jList.setVisibleRowCount(8);
JScrollPane scrollPane = new JScrollPane(jList);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
addButton.addActionListener(e -> addActionPerformed(e));
addButton.setMnemonic(KeyEvent.VK_A);
JPanel buttonPanel = new JPanel();
buttonPanel.add(addButton);
setLayout(new BorderLayout());
add(scrollPane);
add(buttonPanel, BorderLayout.PAGE_END);
}
private void addActionPerformed(ActionEvent e) {
Window window = null;
if (childDialog == null) {
window = SwingUtilities.getWindowAncestor(this);
if (window == null) {
return;
}
childDialog = new JDialog(window, "Child GUI", ModalityType.APPLICATION_MODAL);
childDialog.add(childPanel);
childDialog.pack();
childDialog.setLocationRelativeTo(window);
}
if (childDialog != null) {
childDialog.setVisible(true);
String text = childPanel.getText();
if (!text.trim().isEmpty()) {
listModel.addElement(text);
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
JFrame mainGui = new JFrame("Main GUI");
mainGui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ParentGuiPanel mainPanel = new ParentGuiPanel();
mainGui.add(mainPanel);
mainGui.pack();
mainGui.setLocationRelativeTo(null);
mainGui.setVisible(true);
});
}
}
#SuppressWarnings("serial")
class ChildGuiPanel extends JPanel {
private JTextField textField = new JTextField(15);
private JButton okButton = new JButton("OK");
private JButton cancelButton = new JButton("Cancel");
public ChildGuiPanel() {
okButton.addActionListener(e -> okActionPerformed(e));
cancelButton.addActionListener(e -> cancelActionPerformed(e));
textField.addActionListener(e -> okActionPerformed(e));
okButton.setMnemonic(KeyEvent.VK_O);
cancelButton.setMnemonic(KeyEvent.VK_C);
add(new JLabel("Text: "));
add(textField);
add(okButton);
add(cancelButton);
}
public String getText() {
return textField.getText();
}
private void disposeWindow() {
textField.selectAll();
Window window = SwingUtilities.getWindowAncestor(this);
if (window != null) {
window.dispose();
}
}
private void okActionPerformed(ActionEvent e) {
disposeWindow();
}
private void cancelActionPerformed(ActionEvent e) {
textField.setText("");
disposeWindow();
}
}
I tried to make the first JPanel disappear and the second JPanel visible with the click of a JButton.
So far i only get the first JPanel to show and after clicking the JButton the Frame gets empty.
I also tried to do it with composition so i dont have to extend classes. So my bad understanding of how
composition works might be the problem. I looked into it alot but couldnt find a proper solution for my problem.
First JPanel class:
import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
public class Panel1 {
JPanel firstscreenpanel = new JPanel();
JButton jButton1 = new JButton();
Panel1() {
jButton1.setBounds(300,300,400,200);
jButton1.setBackground(Color.BLACK);
jButton1.setVisible(true);
jButton1.addActionListener(new ActionListener() {
Panel2 test = new Panel2();
public void actionPerformed(ActionEvent e) {
firstscreenpanel.setVisible(false);
test.secondscreenpanel.setVisible(true);
}
});
}
public Component panelone() {
firstscreenpanel.setSize(1280, 1024);
firstscreenpanel.setLayout(null);
firstscreenpanel.setBackground(Color.BLUE);
firstscreenpanel.add(jButton1);
firstscreenpanel.setVisible(true);
return firstscreenpanel;
}
}
Second JPanel class:
import java.awt.Color;
import java.awt.Component;
import javax.swing.JButton;
import javax.swing.JPanel;
public class Panel2 {
public JPanel secondscreenpanel = new JPanel();
public JButton jButton2 = new JButton();
Panel2() {
jButton2.setBounds(100,100,400,200);
jButton2.setBackground(Color.BLACK);
jButton2.setVisible(true);
}
public Component paneltwo() {
secondscreenpanel.setSize(1280, 1024);
secondscreenpanel.setLayout(null);
secondscreenpanel.add(jButton2);
secondscreenpanel.setBackground(Color.RED);
secondscreenpanel.setVisible(false);
return secondscreenpanel;
}
}
JFrame Class:
import javax.swing.JFrame;
public class Frame1 {
public JFrame frame1 = new JFrame();
Panel1 panel1 = new Panel1();
Panel2 panel2 = new Panel2();
Frame1() {
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.setState(JFrame.MAXIMIZED_BOTH);
frame1.setSize(1280, 1024);
frame1.setLayout(null);
frame1.add(panel1.panelone());
frame1.add(panel2.paneltwo());
frame1.setVisible(true);
}
}
Main Class:
public class MainClass {
private void showGUI() {
Frame1 jframe = new Frame1();
}
public static void main(String[] args) {
final MainClass main = new MainClass();
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run() {
main.showGUI();
}
});
}
}
I did not check the whole code (too bug, too many empty lines) but stopped at Panel2 test = new Panel2();
is this instance being added to some visible component? if not it will never be displayed.
Note: using a null layout manager is often not recommended, use a CardLayout or even a JTabbedPane to switch components - see tutorial A Visual Guide to Layout Managers
This is not the best implementation, but it is simple enough for you to follow. I modified your code to create a frame containing your original two panels (although those panel classes are not necessary - as I explained in a comment on your posted solution), and a button to toggle visibility on the panels. I am using a regular JButton and not a JToggleButton also not the best use of the class, but simply for you to understand.
The Action Listener is added to the button on the frame. Notice that my action listener does not create new instances of anything. That was part of the original problem. Since the button is a member of the frame class like the panels 1 and 2, it has access to them directly. SO, in the listener, all I need to do is "toggle" the visibility of each of the panels.
public class Frame1 extends JFrame {
private Panel1 panel1 = new Panel1();
private Panel2 panel2 = new Panel2();
private JPanel btnPanel = new JPanel();
private JButton button = new JButton("Toggle");
public Frame1() {
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
boolean visible = panel1.isVisible();
panel1.setVisible(!visible);
panel2.setVisible(visible);
}
});
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setState(JFrame.MAXIMIZED_BOTH);
setSize(1280, 1024);
btnPanel.setSize(400, 100);
btnPanel.add(button);
setLayout(null);
add(panel1);
add(panel2);
add(btnPanel);
}
}
public class Panel1 extends JPanel {
public Panel1() {
setBounds(100,100,400,200);
setBackground(Color.RED);
setVisible(true);
}
}
public class Panel2 extends JPanel {
public Panel2() {
setBounds(100,100,400,200);
setBackground(Color.BLACK);
setVisible(false);
}
}
public class MainClass {
private void showGUI() {
Frame1 jframe = new Frame1();
jframe.setVisible(true);
}
public static void main(String[] args) {
final MainClass main = new MainClass();
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
main.showGUI();
}
});
}
}
You can fix your program by passing a reference of your frame to your panel1. See my example below.
Frame class
import javax.swing.*;
public class Frame1 {
private JFrame frame = new JFrame();
private Panel1 panel1;
private Panel2 panel2;
Frame1() {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setState(JFrame.MAXIMIZED_BOTH);
frame.setSize(1280, 1024);
frame.setLayout(null);
panel1 = new Panel1(this);
frame.add(panel1.getPanel());
panel2 = new Panel2();
frame.add(panel2.getPanel());
frame.setVisible(true);
}
public Panel2 getPanel2() {
return panel2;
}
}
Panel1 class
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Panel1 {
private JPanel panel = new JPanel();
private Frame1 frame1;
private JButton jButton1 = new JButton();
public Panel1(Frame1 frame1) {
this.frame1 = frame1;
panel.setSize(1280, 1024);
panel.setLayout(null);
panel.setBackground(Color.BLUE);
panel.add(jButton1);
panel.setVisible(true);
jButton1.setBounds(300,300,400,200);
jButton1.setBackground(Color.BLACK);
jButton1.setVisible(true);
jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
panel.setVisible(false);
frame1.getPanel2().getPanel().setVisible(true);
}
});
}
public JPanel getPanel() {
return panel;
}
}
Panel2 class
import javax.swing.*;
import java.awt.*;
public class Panel2 {
private JButton jButton2 = new JButton();
private JPanel panel = new JPanel();
public Panel2() {
panel.setSize(1280, 1024);
panel.setLayout(null);
panel.add(jButton2);
panel.setBackground(Color.RED);
panel.setVisible(false);
jButton2.setBounds(100,100,400,200);
jButton2.setBackground(Color.BLACK);
jButton2.setVisible(true);
}
public JPanel getPanel() {
return panel;
}
}
I solved my problem by adding "public static" to "Panel2 panel2 = new Panel2();"
And then i just used:
"Frame1.panel2.secondscreenpanel.setVisible(true);"
inside the JButton ActionListener.
Its now working but i guess thats a bad way of doing it. Because i heard that using to much static isnt that good. But i dont now why yet.
Okay I can get text fields and normal text and even images to show but I can not get a button to show. I am not sure what I am doing wrong because I have done the same steps for the rest. Any help would be great thanks!
package EventHandling2;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import EventHandling.GUITest;
public class EventMain extends JFrame{
private JLabel label;
private JButton button;
public static void main(String[] args) {
EventMain gui = new EventMain ();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // when click x close program
//gui.setSize(600, 300);
gui.setVisible(true);
gui.setTitle("Button Test");
}
public void EventMain(){
setLayout(new FlowLayout());
button = new JButton ("click for text");
add(button);
label = new JLabel ("");
add(label);
Events e = new Events();
button.addActionListener(e);
}
public class Events implements ActionListener {
public void actionPerformed(ActionEvent e) {
label.setText("Now you can see words");
}
}
}
The problem is with the method: void EventMain()
Constructor has NO return type. Just remove "void". The code will work just fine.
Your actionListener(e) contains a minor control structure error:
public void actionPerformed(ActionEvent e) {
label.setText("Now you can see words");
}
Change to:
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button) {
label.setText("Now you can see words");
}
}
First off, you have to remove void keyword in EventMain's constructor. Then, creating JPanel and add components into it, then add the JPanel to the JFrame.contentPane.
The following code should work:
public class EventMain extends JFrame {
private final JLabel label;
private final JButton button;
public static void main(String[] args) {
EventMain gui = new EventMain();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // when click x
// close program
gui.setSize(600, 300);
gui.setTitle("Button Test");
gui.setVisible(true);
}
public EventMain() {
// setLayout(new FlowLayout());
JPanel panel = new JPanel(new FlowLayout());
button = new JButton("click for text");
panel.add(button);
label = new JLabel("");
panel.add(label);
Events e = new Events();
button.addActionListener(e);
this.getContentPane().add(panel);
}
public class Events implements ActionListener {
public void actionPerformed(ActionEvent e) {
label.setText("Now you can see words");
}
}
}
I'm trying to create a small GUI, it has 2 JButtons, and 2 JPanels with some drawing animation on each of them. By default it must show first JPanel, and by clicking on second JButton I want to see my second JPanel. So : I create JFrame, Panel1 and Panel2, where I have drawn my animations, create Button1 and Button2 and adding to them ActionListeners. I have also MainPanel which has in a fields variable i. By changing this "i" my constructor adds to MainPanel either Panel1 (default) or Panel2 (by clicking on JButton2 I change i). Than I add this MainPanel to my frame. So my question : in the class MainPanel I have refreshMe method, what should I write there to make my GUI working properly? Thanks. Here is my code:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GuiTest {
public static void main(String[] args) {
JFrame f = new JFrame();
MainPanel myPanel = new MainPanel();
f.add(myPanel);
Button1 button1 = new Button1();
Button2 button2 = new Button2();
myPanel.add(button1);
myPanel.add(button2);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
}
}
class MainPanel extends JPanel {
Panel1 p1 = new Panel1();
Panel2 p2 = new Panel2();
public int i = 1; //this is being changed later by clicking JButton
// I use this setter later in actionPerformed in order to change i
public void setI(int i) {
this.i = i;
}
MainPanel() {
if (i == 1) {
this.add(p1);
}
if (i == 2) {
this.add(p2);
}
}
public void refreshMe() {
// Need some help here:
// I don't know what should I write, how to make a repaint of myPanel?
System.out.println("just test, if the method refreshMe working by clicking some button");
}
}
class Panel1 extends JPanel {
public Panel1() {
this.setBackground(Color.BLUE);
// a lot of drawing stuff going on here
}
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
class Panel2 extends JPanel {
public Panel2() {
this.setBackground(Color.GREEN);
// a lot of drawing stuff going on here
}
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
class Button1 extends JButton {
MainPanel someObj1 = new MainPanel();
Button1() {
setText("Show Annimation A");
addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
someObj1.setI(1);
System.out.println("The variable i is now: " + someObj1.i);
someObj1.refreshMe();
}
});
}
}
class Button2 extends JButton {
MainPanel someObj2 = new MainPanel();
Button2() {
setText("Show Annimation B");
addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
someObj2.setI(2);
System.out.println("The variable i is now: " + someObj2.i);
someObj2.refreshMe();
}
});
}
}
In order to reflect changes after adding/removing or resizing a component that is on a visible container call revalidate() and repaint() on the containers instance after adding/removing or resizing the component.
Though this will not work in your code the main reason being inside JButton classes you recreate a new instance of MainPanel when in fact the 2 JButtons should share the single instance which is being used (you could pass MainPanel instance to the JButtons constructors, but you shouldn't really be extending a JButton unless adding custom functionality):
class Button2 extends JButton {
MainPanel someObj2 = new MainPanel();//you create an instance of MainPanel which isnt even showing and than do changes on that, this way you will never see any of the changes
Button2() {
}
}
A few other suggestions on your code:
Dont extend JButton class unnecessarily, simply create an instance of JButton like you did with JFrame and call methods on JButton instance.
Dont forget to create/manipulate Swing components on Event Dispatch Thread, via SwingUtilities.invokeLater(..) block, read here for more.
Here is your code fixed (above suggestions ect implemented):
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Test {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
JFrame f = new JFrame();
final MainPanel myPanel = new MainPanel();
f.add(myPanel);
JButton button1 = new JButton("Show Animation A");
JButton button2 = new JButton("Show Animation B");
button1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
myPanel.setI(1);
System.out.println("The variable i is now: " + myPanel.i);
myPanel.refreshMe();
}
});
button2.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
myPanel.setI(2);
System.out.println("The variable i is now: " + myPanel.i);
myPanel.refreshMe();
}
});
myPanel.add(button1);
myPanel.add(button2);
myPanel.checkPanel();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
}
});
}
}
class MainPanel extends JPanel {
Panel1 p1 = new Panel1();
Panel2 p2 = new Panel2();
public int i = 1; //this is being changed later by clicking JButton
// I use this setter later in actionPerformed in order to change i
public void setI(int i) {
this.i = i;
}
public void refreshMe() {
checkPanel();
revalidate();
repaint();
// Need some help here:
// I don't know what should I write, how to make a repaint of myPanel?
System.out.println("just test, if the method refreshMe working by clicking some button");
}
public void checkPanel() {
if (i == 1) {
this.add(p1);
this.remove(p2);//or it will remain there as this is default flowlayout
} else if (i == 2) {
this.add(p2);
this.remove(p1);
}
}
}
class Panel1 extends JPanel {
public Panel1() {
this.setBackground(Color.BLUE);
// a lot of drawing stuff going on here
}
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
class Panel2 extends JPanel {
public Panel2() {
this.setBackground(Color.GREEN);
// a lot of drawing stuff going on here
}
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
However Id suggest something simpler, fortunately you have 2 choices:
1) Use CardLayout which will allow you to flip between multiple components on a single JFrame/container.
Here is an example I made:
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Test {
private final static String PANEL1 = "panel 1";
private final static String PANEL2 = "panel 2";
public Test() {
initComponents();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new Test();
}
});
}
private void initComponents() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel1 = new JPanel();
panel1.add(new JLabel("Panel 1"));
JPanel panel2 = new JPanel();
panel2.add(new JLabel("Panel 2"));
//Create the panel that contains the "cards".
final JPanel cards = new JPanel(new CardLayout());
cards.add(panel1, PANEL1);
cards.add(panel2, PANEL2);
//create button to allow chnage to next card
JButton buttonNext = new JButton(">");
buttonNext.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent ae) {
CardLayout cl = (CardLayout) (cards.getLayout());//get cards
cl.next(cards);
}
});
//create button to allow chnage to previous card
JButton buttonPrev = new JButton("<");
buttonPrev.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent ae) {
CardLayout cl = (CardLayout) (cards.getLayout());//get cards
cl.previous(cards);
}
});
//create panel to hold buttons which will allow switching between cards
JPanel buttonPanel = new JPanel();
buttonPanel.add(buttonPrev);
buttonPanel.add(buttonNext);
frame.add(cards);
frame.add(buttonPanel, BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
}
}
2) Use removeAll() technique i.e call frame.getContentPane().removeAll() which will remove all components currently on JFrame and than add the new content and call revalidate() and repaint() (also might want to add pack() in there) on JFrame instance to reflect changes. Though Id recommend CardLayout.
I think you can just use CardLayout to implement your function. Please refer to here
I want to check the event of panel class which is being added on the JFrame class. In this sample program there is a button on a panel.
I want to monitor the click event of the button from the source frame.
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
class test extends JFrame implements ActionListener {
test() {
Container cp = this.getContentPane();
JButton b1 = new JButton("add");
cp.add(b1);
b1.addActionListener(this);
}
public void actionPerformed(ActionEvent ae) {
if (ae.getActionCommand().equals("add")) {
panel1 frm = new panel1();
cp.add(frm);
}
}
public static void main(String args[]) {
test t1 = new test();
t1.show(true);
}
}
class panel1 extends JPanel {
panel1() {
JButton b1 = new JButton("ok");
add(b1);
}
}
You need to make the JButton available to the "out side" world some how.
I, personally, would be reluctant to make the button itself available, instead, I would allow the outside world the ability to to attach a ActionListener to it...
public class Test extends JFrame implements ActionListener {
public Test() {
Container cp = this.getContentPane();
JButton b1 = new JButton("add");
cp.add(b1);
b1.addActionListener(this);
}
public void actionPerformed(ActionEvent ae) {
if (ae.getActionCommand().equals("add")) {
TestPane frm = new TestPane();
frm.addActionListener(...); // Add your new action listener here
cp.add(frm);
}
}
public static void main(String args[]) {
test t1 = new test();
t1.show(true);
}
}
public class TestPane extends JPanel {
private JButton b1;
public TestPane() {
b1 = new JButton("ok");
add(b1);
}
public void addActionListener(ActionListener listener) {
b1.addActionListener(listener);
}
public void removeActionListener(ActionListener listener) {
b1.removeActionListener(listener);
}
}
whatever you put in frame it just put into the center of the frame. So use BorderLayout for this to be visible as below
public void actionPerformed(ActionEvent ae) {
if (ae.getActionCommand().equals("add")) {
System.out.println("in actionPerformed");
panel1 frm = new panel1();
// this.removeAll();
add(frm,BorderLayout.NORTH);
this.validate();
}
}