I would like to remove an old JPanel from the Window (JFrame) and add a new one. How should I do it?
I tried the following:
public static void showGUI() {
JFrame frame = new JFrame("Colored Trails");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(partnerSelectionPanel);
frame.setSize(600,400);
frame.setVisible(true);
}
private static void updateGUI(final int i, final JLabel label, final JPanel partnerSelectionPanel) {
SwingUtilities.invokeLater(
new Runnable() {
public void run() {
label.setText(i + " seconds left.");
}
partnerSelectionPanel.setVisible(false); \\ <------------
}
);
}
So, my code update the "old" JPanel and them it makes the whole JPanel invisible. It was the idea. But it does not work. The compiler complains about the line indicated with "<------------". It writes: <identifier> expected, illegal start of type
.
ADDED:
I have managed to do what I needed and I do it in the following way:
public static void showGUI() {
frame = new JFrame("Colored Trails");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(partnerSelectionPanel);
//frame.add(selectionFinishedPanel);
frame.setSize(600,400);
frame.setVisible(true);
}
public static Thread counter = new Thread() {
public void run() {
for (int i=4; i>0; i=i-1) {
updateGUI(i,label);
try {Thread.sleep(1000);} catch(InterruptedException e) {};
}
partnerSelectionPanel.setVisible(false);
frame.add(selectionFinishedPanel);
}
};
It works but it does not look to me as a safe solution. First, I "change" and "add to" the elements of the JFrame from another thread. Is it OK? Then I am not sure that it is OK to add a new JPanel to a JFrame, after I have already "packed" the JFrame and made it visible.