I have a panel on my frame .and by clicking on a button I want to delete the old panel and make the other panel and add that panel to my frame.(also I use netbeans) would you please help me that how can i do that?thanks
Asked
Active
Viewed 1.1k times
0
-
I want to do this at the run time.thanks – Johanna Mar 04 '10 at 17:09
-
3You really better accept Adamski's answer, it appears he's desperate for the points (aiming for 10K?) since he's the only one who continues to offer help even though its now about 15 postings since you've last bothered to accept an answer. By the way the code posted won't work, but hopefully you will at least learn from the concepts presented and figure out the problem on your own. You can also read the tutorials which you've been pointed to many times. – camickr Mar 04 '10 at 17:24
-
Why won't the code work? – Adamski Mar 04 '10 at 17:28
-
Sorry, I should have said the code won't compile (the concept is good) and I know the OP will try to copy word for word without making any effort to understand what the code is doing. – camickr Mar 04 '10 at 17:41
2 Answers
4
JFrame frame = new JFrame();
final JPanel origPanel = new JPanel();
frame.add(origPanel, BorderLayout.CENTER);
MouseListener ml = new MouseAdapter() {
public void mouseClicked(MouseEvent evt) {
// Mouse clicked on panel so remove existing panel and add a new one.
frame.remove(origPanel);
frame.add(createNewPanel(), BorderLayout.CENTER);
// Revalidate frame to cause it to layout the new panel correctly.
frame.revalidate();
// Stop listening to origPanel (prevent dangling reference).
origPanel.removeMouseListener(this);
}
}
origPanel.addMouseListener(ml);

Adamski
- 54,009
- 15
- 113
- 152
-
thanks I have written your code in my Frame and I use "this" instead of "frame",is this correct?? also I can not write this.revalidate(). please help me thanks – Johanna Mar 04 '10 at 17:30
-
1I knew you would come back for spoon feeding trying to fix your compile error. Can you not do any thinking on your own? Do you not know how to use the API to look up valid methods and the Objects they apply to? – camickr Mar 04 '10 at 17:33
-
-
1Frustrating when people ignore you isn't it? You have continued to ignore all my suggestions over the months. You still haven't posted your SSCCE showing what you are doing. I could probably give you the solution and tell you its a "2 character" solution to your problem. But I'm not going to be that generous until you learn how to properly ask a question (which means include a SSCCE with every question) and use the forum (which means accept answers when they are given). – camickr Mar 04 '10 at 21:19
0
This way:
final JFrame frame = new JFrame();
frame.setSize(200, 200);
final JPanel panelA = new JPanel();
final JPanel panelB = new JPanel();
JButton button = new JButton("Switch");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.remove(panelA);
frame.add(panelB);
frame.show();
}
});
JLabel label = new JLabel("This is panel B. Panel A is gone!");
panelB.add(label);
panelA.add(button);
frame.add(panelB);
frame.add(panelA);
frame.show();

rodrigoap
- 7,405
- 35
- 46