my dog learned polymorphism*
The moose likes Swing / AWT / SWT and the fly likes UI troubles Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "UI troubles" Watch "UI troubles" New topic
Author

UI troubles

Chris Ramsey
Ranch Hand

Joined: Sep 15, 2003
Posts: 34
Hello,
I have a problem with my UI app that I cannot seem to resolve. I've tried a couple solutions from previous posts but still have not resolved this issue. Here is the scenario. I have a class that processes a file called
Processor. The method that processes the file is called loadFile(file). The class has a thread called processThread that is created each time the loadFile() method is called. In the main class a method called openFile() allows the user to select a file to process. The openFile method is invoked when the user clicks a "Open File" menu button on the menu. After the file is selected a label on the UI should be updated, showing which file was selected and then call the processor.loadFile(file) method on the file that was selected. So the workflow is to...
1. Get the file
2. Update the label on the GUI with the selected file's name
3. Process the file in the background
The problem is that the UI does not seems to update before the processing starts. So while the file is processing a section of my UI that is the size of the JFileChooser disappears. Since the processor.loadFile(file) spawns a new thread I did not expect to have a resource conflict here. I have tried using the SwingWorker and most currently the SwingUtilities.invokeLater() but the problem still exists. I don't know if this is a Swing issue or a Thread issue. Can someone show me a way to accomplish processing the file only after the label is updated? I have listed the openFile() and loadFile(file) methods below. Thank you for you help. Also will my Processor.loadFile(file) method still run in it's own thread even though its called through the menubutton's actionListener that's calls the openFile() method?


[ March 02, 2004: Message edited by: Chris Ramsey ]
[ March 02, 2004: Message edited by: Chris Ramsey ]
[ March 02, 2004: Message edited by: Chris Ramsey ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi Chris,

// new Thread created
processThread = new Thread(doWork);
processThread.run();

That bold line is the problem. You don't call run() on a Thread to make it go; you call start(). The start() method contains the magic that kicks off a new thread; that new thread turns around and calls run() on your behalf. If you call run() yourself, then it just executes in the thread that called it; in this case, that's the event thread, which explains why your GUI is unresponsive until the file is loaded.
Change the call to run() into a call to start(), and you'll notice a big change in behavior!


[Jess in Action][AskingGoodQuestions]
Chris Ramsey
Ranch Hand

Joined: Sep 15, 2003
Posts: 34
Thank you Mr. Friedman-Hill. Once again you are the man!!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: UI troubles