how to disable a JButton while an op completes?

S

supermail99

After the user presses the jbutton I want to disable the JButton until
an operation completes, at which time I want to re-enable the button.
Simply ignoring events would be OK.

There is a long operation to complete and I don't want the program
queueing button presses from impatient users. How can I do this?
 
R

Roedy Green

There is a long operation to complete and I don't want the program
queueing button presses from impatient users. How can I do this?

just disable the button with thatButton.setEnabled( false );

Then do your work with a different thread, then use invokeLater when
you are done to reenable it. You don't want to tie up the Swing thread
during the long time your process takes. Users will think it has hung
if EVERYTHING becomes totally unresponsive.

see StringWorker in http://mindprod.com/jgloss/thread.html
 
Z

zero

(e-mail address removed) wrote in @g44g2000cwa.googlegroups.com:
After the user presses the jbutton I want to disable the JButton until
an operation completes, at which time I want to re-enable the button.
Simply ignoring events would be OK.

There is a long operation to complete and I don't want the program
queueing button presses from impatient users. How can I do this?

The easiest way would be something like this:

myButton.addActionListener(new ActionListener()
{
actionPerformed(ActionEvent e)
{
myButton.setEnabled(false);
doTask();
myButton.setEnabled(true);
}
});

If you're using threads you'll probably need a different scheme.
 
R

Roedy Green

myButton.setEnabled(false);
doTask();
myButton.setEnabled(true);

He talked of impatient users, so I don't think it wise to do this on
the Swing thread.
 
S

supermail99

myButton.setEnabled(false);
doTask();
myButton.setEnabled(true);

Simply doing this does not work. I don't understand why. Java still
alllows me to press the button while doTask is executing. Please
explain this.
 
I

iamfractal

(e-mail address removed) skrev:
Simply doing this does not work. I don't understand why. Java still
alllows me to press the button while doTask is executing. Please
explain this.

Please supply ballsack.

..ed
 
R

Roedy Green

Simply doing this does not work. I don't understand why. Java still
alllows me to press the button while doTask is executing. Please
explain this.

that is because you tied up the event thread
with doTask so Swing could not do any painting.

See http://mindprod.com/jgloss/swingthreads.html
and look for SwingWorker. You can us ethat to spin off doTask on
another thread allowing the GUI to paint the setEnabled(false), done
when the repaint event pops to the top of the queue not here.

The last thing doTask should do (or its caller should do) is call
SwingUtilities.invokeLater the setEnabled(true). that has to go on the
Swing thread not the doTask thread.
 

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,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top