• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JButton update JLabel before running method in another class

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)?
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic