Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JButton update JLabel before running method in another class

 
Raymond Kelly
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using JDK 1.7 Swing Applet.
The code below is a simple applet that calls a Parser.parse() method.
I have a simple JLabel that I want to update with "Processing...Please wait!" before the parse() method is run and then update it again after the parse() method is complete.
In my btnParse actionlistener, you will see the update of the JLabel, however, the label is only getting updated after the parse() method completes. It does not update the label before it starts.

Any help would be appreciated.
Raymond

 
Campbell Ritchie
Sheriff
Posts: 51450
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Why are you using an applet? They are obsolete.
Does that code compile? I can't seem to see where you declared file. Where does the Parser class come from? Why are you calling the parse method as if it were static? Why have you get the keyword static so often in your program? Why are you throwing an exception and catching it in the same method?
Those are all errors which you should correct first.

When you set the text in the label, are you repainting the display? If everything is running in the same thread, you may not be able to repaint it because the thread will be busy. Find out about worker threads, which may help with that problem.
 
Raymond Kelly
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the response.
I guess I should have cleaned up my code before posting it.

Repaint was not working since it was all in the same thread. I created a worker thread and that worked like a charm.
Thank you.

What should I be using instead of JApplet (using 1.7)?
 
Dave Tolls
Ranch Hand
Posts: 2112
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


All of this code happens on the EDT (Event Dispatch Thread), which is essentially the thread that Swing does all its work on. I think the link Campbell posted talks about it.

So what's happening here (inside the action listener code that is called when the event is raised) is the text is set, which flags a redraw event, which cannot be actioned until the current event is finished. Then the parse code runs, still on the same thread, followed by the second setText(), adding a possible additional redraw event (though it may be smart enough to know there's one in the queue). Only after exiting all this, freeing up the EDT to handle the next event, will Swing have a chance to actually draw anything.

This is why workers exist. They do the work on another thread, freeing up the EDT.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic