Swing - Multiple GUIs
I am developing an application with multiple Swing GUIs. There is a button in the main GUI that, when clicked, brings up another GUI. The problem is that both windows freeze after clicking this button and a new GUI appears.
I have looked in SwingUtilities.invokeLater
, but I cannot use it to create the first GUI as I am passing it an object reference, which I do not want it to be "final" as a compiler requirement.
The first GUI is created with
MainUI gui = new MainUI(player);
gui.setVisible(true);
The second one is created with:
private void challengeBtnActionPerformed(java.awt.event.ActionEvent evt) {
if (board.isVisible()) {
board.dispose();
resetComponents();
} else {
MainUI gui = new MainUI(player);
gui.setVisible(true);
}
}
Can you help me?
source to share
Once again, you shouldn't interact with Swing components from any thread other than EDT .
You should call MainUI
like this:
public class MainUI extends javax.swing.JFrame {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new MainUI().setVisible(true);
}
});
}
}
You can modify your application so that it can call yours MainUI
this way.
UPDATE:
The following code should work.
public class MainUI extends javax.swing.JFrame {
public static void main(String[] args) {
final Player player = new Player();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new MainUI(player).setVisible(true);
}
});
player.changeState(); // You can do this
// player = new Player(); // You can't do that
}
}
source to share