How do I update a JFrame?
I'm trying to switch from one JPanel to another in my JFrame and I just can't get it to Update the JFrame. I've reproduce a lighter version of my code to make it easyer to read. The original one got more stuff and the JPanel with Events are in seperate files.
Here's the simple version of the code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class test {
public static JFrame frame = new JFrame ("MyPanel");
public static JPanel MyPanel1() {
JPanel jp = new JPanel();
JButton page1BTN = new JButton ("Page 1");
page1BTN.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
page1BTN_Clicked(e);
}
});
jp.setPreferredSize (new Dimension (234, 175));
jp.setLayout (null);
jp.add (page1BTN);
page1BTN.setBounds (55, 70, 100, 20);
return jp;
}
public static JPanel MyPanel2() {
JPanel jp = new JPanel();
JButton page2BTN = new JButton ("Page 2");
page2BTN.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
page2BTN_Clicked(e);
}
});
jp.setPreferredSize (new Dimension (234, 175));
jp.setLayout (null);
jp.add (page2BTN);
page2BTN.setBounds (55, 70, 100, 20);
return jp;
}
protected static void page2BTN_Clicked(ActionEvent e) {
frame.getContentPane().removeAll();
frame.getContentPane().add (MyPanel1());
frame.validate();
}
protected static void page1BTN_Clicked(ActionEvent e) {
frame.getContentPane().removeAll();
frame.getContentPane().add (MyPanel1());
frame.validate();
}
public static void main (String[] args) {
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add (MyPanel1());
frame.pack();
frame.setVisible (true);
}
}