• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Swing text label updates too late

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm new to Java and also new to Swing but if this post should be in the Swing forum, please let me know.

My program has a class called Gui which extends JFrame. Gui has a panel at the bottom called statusBar which has a label on it called status. Gui has a public method defined as:

I use this for the obvious reason: showing the current status at various points during the program according to user interaction.

There is also a menu and when the user selects one of the menu items a method is called which runs an external command using the following code inside a try-catch:

where cmd is the command to run in String format. The ellipsis (...) represents some stream-handling code designed to redirect the output of the command into a file. The exitVal isn't used.

Now, the problem is that when the user selects this menu item the very first thing I do before calling the external command is to show what's happening on the status line with:

where gui is the only instance of Gui in the program. And what's happening is that the text isn't being shown until after the external command has finished.

I don't know if it makes a difference but at the moment, the code for gui is running on the main thread as this is mostly test code.

How can I get the text on the status label to show while the external command is running?
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm sure you've read:
- When Runtime.exec() won't
- Concurrency in Swing
 
Garry Knight
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've read the first one. It's what I based my code on. That part of the code has been running without problem for some time. I decided to add some status messages around this code just recently, which is where I ran into the problem.

So now I'll read the concurrency articles, though I suspect at this point that instead of using

in my main(), I'll have to put it in its own thread with SwingUtilities.invokeLater, but I won't jump the gun, I'll take the time to read it thoroughly. Thanks for your reply. :-)
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Garry Knight wrote:. . . if this post should be in the Swing forum, please let me know. . . .

I think it probably would fit better into the Swing forum, so shall move it.
 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Garry Knight wrote:I've read the first one. It's what I based my code on. That part of the code has been running without problem for some time. I decided to add some status messages around this code just recently, which is where I ran into the problem.

So now I'll read the concurrency articles, though I suspect at this point that instead of using

in my main(), I'll have to put it in its own thread with SwingUtilities.invokeLater, but I won't jump the gun, I'll take the time to read it thoroughly. Thanks for your reply. :-)



That is a good start as it will ensure that your UI itself is properly started on the event dispatching thread (EDT) but you also need to ensure that anything that updates the UI later also happens on the EDT. So you probably need to add some similar code to your status updating method. Even if you start the GUI on the EDT, if another thread (say the main thread maybe) calls your setStatus() method, the update isn't going to happen properly on the EDT.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I'll have to put it in its own thread with SwingUtilities.invokeLater


SwingUtilities#invokeLater(...) does not start a new thread. Read the concurrency tutorial and the documentaion for the method and you'll easily see what it really does.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic