Meaningless Drivel is fun!*
The moose likes Swing / AWT / SWT and the fly likes ActionListener and text problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "ActionListener and text problem" Watch "ActionListener and text problem" New topic
Author

ActionListener and text problem

Carty Ellis
Greenhorn

Joined: Oct 04, 2003
Posts: 20
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Carty Ellis
Greenhorn

Joined: Oct 04, 2003
Posts: 20
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

Joined: Jul 11, 2001
Posts: 15299
    
    6

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 ]

GenRocket - Experts at Building Test Data
 
GeeCON Prague 2014
 
subject: ActionListener and text problem