I have a Swing UI with certain number of buttons. One button invokes a separate swing class and a new window is opened with new buttons and other features. Now when I am closing this window the original window is also getting closed. I think this is because both the classes have impemented
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
//some code
}
}
Now how to overcome this? A code example or link will be very helpful. Thanks
It is hard to guess without seeing the code. Perhaps your JFrame has setDefaultCloseOperation set to close the application when the window is closed (maybe EXIT_ON_CLOSE). Have a look at the java doc for more info on this.
From the java doc:
EXIT_ON_CLOSE(defined inJFrame): Exit the application using theSystem exitmethod. Use this only in applications.
This means that the whole application will close. You do not want that for the second window. You just want the window (not the application) to close.
SwingUtilities.invokeLater has no relation to an application closing. Its purpose is to defer the run method invocation to when the GUI thread has finished painting and processing current events.
I suggest you to check your code for EXIT_ON_CLOSE as was already suggested and also for System.exit() calls.
To answer the comment, since you set EXIT_ON_CLOSE on both frames, it is the expected behaviour to exit the whole application when you close any of the frames. See Javadoc.
To solve your problem, you need to remove this setDefaultCloseOperation on the second frame.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With