ActionListener

C

Clive Moore

Can anyone shed any light on this?

I have an ActionListener that is added to a JButton.

when the jbutton is pressed the user has to wait for a approx 3 seconds
before a response is returned. Unfortunatly this does not appear to
work.

public ActionListener actionListener = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
button.setEnabled(false);
// perform operation (takes 3 seconds)
temp.setEnabled(true);
}
};

has anyone come accross this problem before and if so is there an easy
way to rectify it?

Thanks
Clive
 
C

Chris Smith

Clive said:
Can anyone shed any light on this?

Maybe. See below.
I have an ActionListener that is added to a JButton.

when the jbutton is pressed the user has to wait for a approx 3 seconds
before a response is returned. Unfortunatly this does not appear to
work.

public ActionListener actionListener = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
button.setEnabled(false);
// perform operation (takes 3 seconds)
temp.setEnabled(true);
}
};

has anyone come accross this problem before and if so is there an easy
way to rectify it?

(I'm assuming that 'button' and 'temp' point to the same button above?
If not, then you've lost me and will need to provide more context.)

Your problem relates to doing to much in the AWT event thread. Remember
that as long as you are using the AWT event thread, no events can be
delivered and nothing is painted.

So, the sequence of events happening here is:

1. The button is pressed.

2. An ActionEvent is delivered to your listener.

3. The button is disabled. As part of that process, a repaint request
is queued to redraw the button to appear disabled. However, because
you're still using the AWT event thread, that repaint doesn't happen
yet, and the button still appears enabled.

4. You perform your long operation. During this time, a mouse click or
two on the button bay be queued to be handled, but these clicks are not
handled yet because you're still using the AWT event thread, so they
remain on the queue to be handled later.

5. The button is enabled again. As part of that process, a repaint
request would be queued to redraw the button to appear enabled... except
that there's still a repaint waiting from the previous disabling of the
button, and the two are collapsed. Still though, when the button is
redrawn now, it will show as enabled.

6. Your listener completes, so the AWT event thread can finally get back
to the event queue and do all this stuff it's been waiting to do.

7. It redraws the button (however, the button doesn't really look any
different than it did before, so you don't see anything.)

8. Any waiting mouse clicks are delivered. Because the button is
enabled, these waiting mouse clicks do have an effect and an ActionEvent
is delivered to your listener again.

The solution: move long tasks outside of the AWT event thread.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
C

Clive Moore

Thanks Chris. You where correct. By kicking of a new thread in the
action
performed method i gained greater control of what was happening i in
each subsequent
event i could check if the thread was alive if it was then i simply
returned
out of the method.

Thanks
Clive
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top