AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

logic in one thread and swing in another

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java GUI Toolkits
View previous topic :: View next topic  
Author Message
Gioele Barabucci
Guest





PostPosted: Tue Jan 25, 2005 12:16 am    Post subject: logic in one thread and swing in another Reply with quote



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)?
Back to top
Frank Liu
Guest





PostPosted: Tue Jan 25, 2005 2:22 am    Post subject: Re: logic in one thread and swing in another Reply with quote



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

"Gioele Barabucci" <barabucc (AT) cs (DOT) unibo.it> wrote

Quote:
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)?



Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java GUI Toolkits All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.