This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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.
Thanks [ September 19, 2004: Message edited by: Tom McAmmond ]
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.
Joined: Feb 16, 2004
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?
Updated: 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 ]