File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Basic invokeLater question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Basic invokeLater question" Watch "Basic invokeLater question" New topic

Basic invokeLater question

Tom McAmmond
Ranch Hand

Joined: Feb 16, 2004
Posts: 58
Hi there everyone....

My app loads up a gui and immediately performs some tasks. What's the best way to load it up? Currently, I do something like this:

I think it's running the execute() method on the Swing thread like this, right? Only I don't want to do that. Bad design. I think that I want to create the gui on a thread separate from the thread that's performing all the lengthy tasks. So the question is, where do I instantiate my class and where/how do I create the gui? I think these three methods are probably the only three that need to be involved, but what goes inside them?

Suggestions? Please? Should be easy for those of you accustomed to Swing - I'm just getting my feet wet.

[ September 19, 2004: Message edited by: Tom McAmmond ]
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Here's a suggestion. You can use invokeLater to update the gui from your background work thread. I added a JProgressBar to show the changes.
Tom McAmmond
Ranch Hand

Joined: Feb 16, 2004
Posts: 58
Thanks for the code (and the suggestion) There are some interesting ideas in there, which I will explore.... But is there another way to do it that doesn't require the long-running-task to launch the creation of the gui? The gui should be created, yes, but I want the main thread to launch it. I could be mistaken, but I don't think that's what this code does.

Currently execute() sets up a couple of things and launches a new thread to perform my long-running task, but during the setup (before launching the thread) it's blocking the gui. Is there another way to stop that from happening?

Upon further research, I have gained a new understanding of Swing threading. Correct me if I'm wrong, but it looks like I should construct my GUI on the main thread, then create a new instance of my class by passing it a reference to my JFrame, then have my class run execute() like so:

Does that look reasonable too?
[ September 19, 2004: Message edited by: Tom McAmmond ]
I agree. Here's the link:
subject: Basic invokeLater question
It's not a secret anymore!