Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swing popup in a Swing Application

Tags:

java

swing

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

like image 304
JavaBits Avatar asked Dec 29 '25 02:12

JavaBits


2 Answers

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 in JFrame): Exit the application using the System exit method. 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.

like image 158
lindon fox Avatar answered Dec 31 '25 14:12

lindon fox


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.

like image 42
Michel Daviot Avatar answered Dec 31 '25 15:12

Michel Daviot



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!