I think this subject is pretty common, but I could not find any solution to
my problem.
Long story short: Is there something I can call inside a class that is in
the main thread so that it is kept alive, or stopped, while a Swing class
is doing its jobs (displaying JFrame and waiting clicks)?
Here is the full scenario: I have one class doing all the business (very
little calculations, workflow decisions) "Application" and other doing the
graphics work "Interface". I have only one instance of each.
Application starts up, sets up the Interface and ask to the it to display
the first page. Then the interface will catch button actions and callback
Application's methods like << application.doOperation3(arguments) >>,
Application would then reply with << interface.displayPage4(results) >>.
Fine design for my little application :)
Here is the problem: the Application instance disappear after it launches
the first iterface.display1stPage(); because it does not wait on anything.
So some seconds later application.doOperation1() fails with a
NullPointerException.
Is there something I can call inside Application so that it is kept alive,
or stopped, while Interface is doing its jobs (displaying JFrame and
waiting clicks)?
Frank Liu - 25 Jan 2005 02:22 GMT
The main application thread that start the gui, ie the public static void
main() method will disappear once it sets up the main gui, ie JFrame and
such. After that the Event dispatch thread, the thread responsible for mouse
clicks, buttons as well as the drawing of the gui becomes the main thread.
This thread never stop and never should stop unless you want your gui
becomes non-reponsive. What you can do is start a new thread in response to
a button click. So the main thread will intercept the button click start a
new thread and immediately return to do what it suppose to do ie repaint gui
and stuff. If you want the GUI thread to "stop" because you want the user to
wait for the application to do it's thing, you'll have to disable the gui
yourself to reflect that, never just stop the main gui and freeze the app.
This can be done using modal dialog box or something like that. Once the new
worker thread has done its thing you can call the main thread via
SwingUtilities.invokeLater() from your worker thread, like close the modal
dialog box. Anything you put in that function will run in the main thread.
Frank
>I think this subject is pretty common, but I could not find any solution to
> my problem.
[quoted text clipped - 21 lines]
> or stopped, while Interface is doing its jobs (displaying JFrame and
> waiting clicks)?