Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

UI troubles

 
Chris Ramsey
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Chris Ramsey
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Mr. Friedman-Hill. Once again you are the man!!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic