Saturday, March 1, 2014

How to update JFrame.


Hi all,


I am creating a program in which I have a JFrame that switches between different panels that are on it. I am using the remove() method of the JFrame to remove the current panel from the frame and then add the new one on. My question is, how can I get the Frame to update without having to hide the window and bring it back into focus? Or is there a different way I should be doing this? Here is some sample code that I have.



Java Code:



public static void main(String[] args) {
Main main = new Main();

JFrame frame = new JFrame("Gradebook Alpha 1.0");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setLayout(new BorderLayout());
frame.add(main, BorderLayout.CENTER);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);

try {
Thread.sleep(1500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
frame.remove(main);
System.out.println("removed");

frame.add(new JButton("A"));
}

Thank you for your time.

No comments:

Post a Comment