• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Swing text label updates too late

 
Garry Knight
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • 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?
 
Rob Spoor
Sheriff
Pie
Posts: 20559
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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. :-)
 
Campbell Ritchie
Sheriff
Pie
Posts: 49472
64
  • Mark post as helpful
  • send pies
  • 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.
 
Alex Hurtt
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • 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.
 
Darryl Burke
Bartender
Posts: 5136
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic