File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes How to make JFrame/JPanel update BEFORE idle time Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to make JFrame/JPanel update BEFORE idle time" Watch "How to make JFrame/JPanel update BEFORE idle time" New topic

How to make JFrame/JPanel update BEFORE idle time

Eric Crockett

Joined: Jan 24, 2005
Posts: 25
I am using a GUI for a program, it is not that complicated. I have a JFrame with a JPanel as it's sole component. There are various components mounted on the JPanel.

The problem comes when I try to DO something. When I click a JButton, a JTextfield is supposed to change from its current value to "Working..." WHILE the task is being performed. Unfortunately, it only changes the value AFTER the action is done and there is "idle time". The code below gives a short example.

Before the following code runs, there is a window open with a textfield with any value in it. Then a JButton is clicked and the code is in the actionlistener.

And here is what happens:
"hello" is printed out
nothing happens for 5 seconds
THEN the GUI is updated and the textfield is changed to "Working...", which rather defeats the purpose of having it there in the first place.

What I found interesting is that hello would print out, but the GUI would not update. Which component do I need to update (the frame or the panel)? How? Where?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
the Event Dispatch Thread (EDT) handles the painting, so when you do a
'long task', the EDT is blocked until the long task is finished.

you need to do your long task in a thread of its own, separate from the EDT

here's a simple demo of your problem (blocking the EDT)

here's the same code, with its own thread, and working correctly
(note the SwingUtilities.invokeLater(), which updates the textfield in the EDT)
Eric Crockett

Joined: Jan 24, 2005
Posts: 25
Thanks, that was EXACTLY what I was looking for. I had no idea you could use new threads at all, much less for what I needed.
Since I'm now learning about threads (per the last post) I was wondering what the major applications are of multi-threaded programs. How and where (in general) could I use them to speed up programs?
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46360
What do you use threads for? Easy. Anything where you do two things simultaneously. I am doing several things simultaneously, eg sitting here, reading the screen, drinking my coffee.
My computer is doing several things, eg displaying my reply, checking for e-mails, checking for viruses in what comes in and out, etc.

Look here in the Java tutorial. Look at the Threads forum on this website. Find any large Java book; threads represent an essential part of any non-basic Java programming.
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
Also take a look at which gives both a short introduction to this specific problem, and a nice solution to it.

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
It is sorta covered in the JavaRanch Style Guide.
subject: How to make JFrame/JPanel update BEFORE idle time
It's not a secret anymore!