Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to make JFrame/JPanel update BEFORE idle time

 
Eric Crockett
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 49411
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also take a look at http://spin.sourceforge.net/ which gives both a short introduction to this specific problem, and a nice solution to it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic