Replacing existing panel with a new panel.
I don't use swing much and I have ran into a small problem.
What is the standard procedure for replacing a jpanel on a
jframe with another jpanel in the exact same position?
Say you had a form on the first panel, when the user clicks
next it display a new panel with a confirmation of the results
and disposes of the old one.
I have tried add,remove, setcontenpane. setvisible etc but nothing
works. Can someone explain?
Here is a small app which demonstrates my problem:
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
publicclass FrameTestextends JFrameimplements ActionListener{
JPanel jp1;
JPanel jp2;
JButton jb1;
JButton jb2;
public FrameTest(String name){
super(name);
init();
}
publicvoid init(){
jp1 =new JPanel(new FlowLayout());
jb1 =new JButton("One");
jb1.addActionListener(this);
jp1.add(jb1);
setContentPane(jp1);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setBounds(0, 0, 200, 200);
setVisible(true);
}
public JPanel panelTwo(){
jp2 =new JPanel(new FlowLayout());
jb2 =new JButton("Two");
jb2.addActionListener(this);
jp2.add(jb2);
return jp2;
}
publicstaticvoid main(String[] args){
FrameTest ft =new FrameTest("Test");
}
/*
* On click I wish to replace panel one
* with panel two
*/
publicvoid actionPerformed(ActionEvent e){
//What can I add here?
}
}
Regards

