Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ActionListener and text problem

 
Carty Ellis
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have written this code:
*/ -- my code
class JconvmrefListener implements ActionListener {
public void actionPerformed(ActionEvent ev) {
text.setText("Jconvmref executing. . .");
Jconvmref.loadMastref();
text.setText("Jconvmref completed\nRun another load or click the 'Close Window' button");
}
}
*/
The SECOND setText appears, but the first never does. ???
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Swing invokes event handlers in the same thread used for screen painting. Therefore while an event handler is running, the screen can't be updated at all. If you want to do some long-running task in response to a user event (and if you've got an "executing..." message, I'm assuming you do) then the right way to do it is to spawn a new thread, and do the long-running task in that thread. This thread can make changes to the GUI (subject to Swing's rules for multithreaded access, of course) and call repaint() to update the GUI as many times as necessary while it's running.
 
Carty Ellis
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the event handler NOT doing three things between the brackets? My intent was that the event handler would setText to the text area, call the method for loading the database file, and lastly, setText with anohter message. Steps 2 and three happen, but not the first - why is it lost?
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Carty Ellis:
Is the event handler NOT doing three things between the brackets? My intent was that the event handler would setText to the text area, call the method for loading the database file, and lastly, setText with anohter message. Steps 2 and three happen, but not the first - why is it lost?

Orginally posted by Ernest Friedman-Hill:
Swing invokes event handlers in the same thread used for screen painting. Therefore while an event handler is running, the screen can't be updated at all.

To be a little more specific, screen repaints do not happen until AFTER the event thread is complete. So it doesn't matter what you are doing in the event brackets. If something needs to be refreshed/repainted it just won't happen until the event thread finishes. Then a repaint gets issued and you will see the changes.
This is obvious when you click a button that performs long running times of work. You should notice that the button doesn't get painted back as unpressed until after the event is complete.
[ April 23, 2004: Message edited by: Gregg Bolinger ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic