Wait for the Swing GUI to close before continuing.
I am writing an application that requires the user to enter some data into the Swing GUI, which the application will then use. After the user enters data, a graphical interface is no longer required, since the application will write some data to files.
The general idea is this:
launchGui(); closeGui(); continueWithComputation();
I understand that Swing uses several threads in the background, which I understand, so the program does not block until the GUI closes.
Is it possible to somehow expect the completion of the GUI (a single JFrame
closed with dispose()
) before continuing with continueWithComputation()
?
Is it possible to somehow wait for the GUI to complete (one JFrame is closed with dispose ()) before continuing with continueWithComput ()?
Add windowListener user actions
from code to calling
JFrame#setVisible(false)
, then you can runcontinueWithComputation()
, you need to close this JVM onSystem.exit(0)
, otherwise it will remain in the RAM of the PC until the restart or power off
Wait for the Swing GUI to close before continuing.
Use a modal dialog. For more information, see the following:
- How to make dialogs
- How to use modality in dialogs
I just had a similar problem, but without closeGui()
and I got this relatively short piece of code using WindowListener and some Java synchronization:
AtomicBoolean closed = new AtomicBoolean(false); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); frame.addWindowListener(new WindowAdapter() { @Override public void windowClosed(WindowEvent e) { synchronized(closed) { closed.set(true); closed.notify(); } super.windowClosed(e); } } ); frame.setVisible(true); synchronized(closed) { while (!closed.get()) { closed.wait(); } } // executes after the Frame has been disposed