Hi All,
I don't know what subject line i should use for my post. But anyways,
this is my problem.
When a button (JButton) is pressed, it's stays pressed till the
actionPerformed() method is completed for that button. So what I did
was, I disabled the button as soon as I entered the actionPerformed()
for this button (so that it gives some indication to the user that
something is happenning in the background), and then I did my work for
the button, and then I enabled the button back.
I thought this will work, but it didn't, the button won't disable and
it still stays pressed... What could be the problem???. Do I have to
call a repaint() or anything like that.
Thank you very much.
Srikanth.
NullBock - 29 Nov 2005 11:53 GMT
The problem is that you are performing lengthy operations in the event
thread. The button doesn't "stay pressed," it's simply that your GUI
isn't being updated as long as you're performing your actions. Think
about running your actions in a worker thread:
public void actionPerformed(ActionEvent event) {
button.setEnabled(false);
Thread t = new Thread() {
public void run() {
//you do that voodoo that you do so well--ie, perform your task
SwingUtilities.invokeLater(new Runnable() {
public void run() {
button.setEnabled(true);
}
});
}
};
t.start();
}
That way, you're only doing GUI things in your event thread, and
time-consuming things in another thread.
Walter Gildersleeve
Freiburg, Germany
______________________________________________________
http://linkfrog.net
URL Shortening
Free and easy, small and green.
Srikanth - 29 Nov 2005 13:38 GMT
Thank you very much, now I know 2 people from Germany. The first one is
Michael Schumacher, and the other one is you. Just kidding, thank you
for the help.
NullBock - 29 Nov 2005 14:53 GMT
Of course, Schumacher lives in Switzerland now to avoid taxes...and I'm
not even German! Sorry to rain on your parade ;)
Walter
Srikanth - 29 Nov 2005 15:36 GMT
I didn't know that Schumacher lives in Swiss, but now I know, and now I
also know that you are not a German! So I'll decrease my counter (
--germanPeopleIKnow; )...
But more importantly I understood the invokeLater() method.
Roedy Green - 29 Nov 2005 17:30 GMT
>I thought this will work, but it didn't, the button won't disable and
>it still stays pressed... What could be the problem???.
you can't do long tasks on the Swing thread.
See http://mindprod.com/jgloss/swingthreads.html

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Srikanth - 30 Nov 2005 06:32 GMT
Yes, even I agree,
But instead all the component changes like setEnabled(), setText()
methods can be used in swing threads while performing the long tasks.