how to disable a JButton while an op completes?

Discussion in 'Java' started by supermail99@fastmail.fm, Sep 29, 2005.

  1. Guest

    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?
    , Sep 29, 2005
    #1
    1. Advertising

  2. Roedy Green Guest

    On 29 Sep 2005 12:34:04 -0700, 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
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
    Roedy Green, Sep 29, 2005
    #2
    1. Advertising

  3. zero Guest

    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.
    zero, Sep 29, 2005
    #3
  4. Roedy Green Guest

    Roedy Green, Sep 30, 2005
    #4
  5. Roedy Green Guest

    On Thu, 29 Sep 2005 20:49:04 GMT, zero <> 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.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
    Roedy Green, Sep 30, 2005
    #5
  6. zero Guest

    Roedy Green <> wrote in
    news::

    > On Thu, 29 Sep 2005 20:49:04 GMT, zero <> 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.


    True, a hanging gui is annoying.
    zero, Sep 30, 2005
    #6
  7. Guest

    > 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.
    , Oct 11, 2005
    #7
  8. Guest

    skrev:

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


    Please supply ballsack.

    ..ed

    --
    www.EdmundKirwan.com - Home of The Fractal Class Composition.
    , Oct 11, 2005
    #8
  9. Roedy Green Guest

    On 11 Oct 2005 07:10:10 -0700, wrote or quoted
    :

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


    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.

    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
    Roedy Green, Oct 13, 2005
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. joeted
    Replies:
    0
    Views:
    322
    joeted
    Apr 21, 2004
  2. Brian Gordaychik
    Replies:
    6
    Views:
    17,057
    John Saunders
    Dec 6, 2004
  3. Basman
    Replies:
    0
    Views:
    428
    Basman
    Oct 29, 2007
  4. Lisa
    Replies:
    0
    Views:
    82
  5. Lisa
    Replies:
    1
    Views:
    79
    Daniel Kirsch
    Dec 15, 2004
Loading...

Share This Page