Will
Will

Reputation: 482

How to stop user from generating more events?

I am developing a java swing desktop application. The dialog form has an ok and cancel button. When the user clicks ok button the application does some processing. How can I stop user from clicking ok again before the event on ok button has finished executing. Also, i dont want the user to able to press the cancel button till ok has finished executed. Any help will be highly appreciated.

Upvotes: 1

Views: 150

Answers (5)

kleopatra
kleopatra

Reputation: 51535

Enablement management is an integral part of UI logic. Action helps you doing so:

 Action action = new AbstractAction("myAction") {

      public void actionPerformed(ActionEvent e) {
           setEnabled(false);
           doPerform();
           setEnabled(true);
      }
 };
 button.setAction(action);

Beware: long running task must not be executed on the EDT, so this is for short-term only, to prevent the second or so click having an effect

Edit

just noticed that you tagged the question with jsr296, then it's even easier: you can tag a method of your presentation model as @Action and bind its enabled property to a property of the model

@Action (enabledProperty == "idle")
public void processOk() {
    setIdle(false);
    doStuff;
    setIdle(true);
}

Plus there is support (much debated, but useable) for Tasks: basically SwingWorker with fine-grained beanified life-cycle support

Upvotes: 5

vaughandroid
vaughandroid

Reputation: 4374

If you want to disable all the controls, then I'd suggest using a GlassPane. See here for more info: http://docs.oracle.com/javase/tutorial/uiswing/components/rootpane.html

Upvotes: 1

Francisco Puga
Francisco Puga

Reputation: 25168

It seems that what you want is something like a waiting cursor.

Upvotes: 0

Petr Mensik
Petr Mensik

Reputation: 27536

You can disable your button like yourButton.setEnabled(false); during your processing method and enable it again when it finishes.

Upvotes: 0

pheromix
pheromix

Reputation: 19347

The easiest way is to disable the button just after clicking it , and when the action performed is complete then re-enable it.

Upvotes: 0

Related Questions