wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes Swing text label updates too late Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Swing text label updates too late" Watch "Swing text label updates too late" New topic
Author

Swing text label updates too late

Garry Knight
Greenhorn

Joined: Nov 08, 2010
Posts: 5
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?

Garry Knight
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

I'm sure you've read:
- When Runtime.exec() won't
- Concurrency in Swing


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Garry Knight
Greenhorn

Joined: Nov 08, 2010
Posts: 5
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

Joined: Oct 13, 2005
Posts: 37879
    
  22
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

Joined: Oct 26, 2010
Posts: 98
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

Joined: May 03, 2008
Posts: 4523
    
    5

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.


luck, db
There are no new questions, but there may be new answers.
 
 
subject: Swing text label updates too late
 
Similar Threads
Install Linux application using Java
FrameView only gets repainted when I resize the window
run dos commands through servlet
two applets
exception java.lang.InterruptedException is never thrown in body of corresponding try statement