Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2005

Tip: Looking for answers? Try searching our database.

how to disable a JButton while an op completes?

Thread view: 
supermail99@fastmail.fm - 29 Sep 2005 20:34 GMT
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?
Roedy Green - 29 Sep 2005 21:44 GMT
On 29 Sep 2005 12:34:04 -0700, supermail99@fastmail.fm wrote or quoted

>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
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

Roedy Green - 30 Sep 2005 05:01 GMT
>see StringWorker in http://mindprod.com/jgloss/thread.html

Oops SwingWorker
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

zero - 29 Sep 2005 21:49 GMT
supermail99@fastmail.fm wrote in news:1128022444.888235.277260
@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.
Roedy Green - 30 Sep 2005 05:02 GMT
On Thu, 29 Sep 2005 20:49:04 GMT, zero <zero@this.hi> wrote or quoted

> 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.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

zero - 30 Sep 2005 13:52 GMT
> On Thu, 29 Sep 2005 20:49:04 GMT, zero <zero@this.hi> wrote or quoted
>
[quoted text clipped - 4 lines]
> He talked of impatient users, so I don't think it wise to do this on
> the Swing thread.

True, a hanging gui is annoying.
supermail99@fastmail.fm - 11 Oct 2005 15:10 GMT
> 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.
iamfractal@hotmail.com - 11 Oct 2005 16:03 GMT
supermail99@fastmail.fm skrev:

> > myButton.setEnabled(false);
> >      doTask();
[quoted text clipped - 3 lines]
> alllows me to press the button while doTask is executing. Please
> explain this.

Please supply ballsack.

.ed

--
www.EdmundKirwan.com - Home of The Fractal Class Composition.
Roedy Green - 13 Oct 2005 05:38 GMT
On 11 Oct 2005 07:10:10 -0700, supermail99@fastmail.fm wrote or quoted

>> myButton.setEnabled(false);
>>      doTask();
[quoted text clipped - 3 lines]
>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.

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.