Tomi S
Tomi S

Reputation: 197

Set JButton so that it cannot be pressed or to temporary disable actionListener

I have actionListener on JButtons and if I press one of them, I want to somehow disable actionlistener on other ones without removing it.

Is it possible or do I have to remove and than add them actionListener again.

Sorry. I forgot to mention, that i set "button.setPressedIcon(icon)" and i don't want to show this icon. So the mentioned soulution - button.setEnabled(false) wont work.

Upvotes: 1

Views: 3263

Answers (3)

Andrew Thompson
Andrew Thompson

Reputation: 168825

Besides disabling GUI elements themselves, you might construct them using the Action (or AbstractAction) class and disable/enable the action instead. Whatever UI elements were formed from the action will be disabled/enabled accordingly.

Upvotes: 2

RMT
RMT

Reputation: 7070

JButton button = new JButton("hello");
button.setEnabled(false)

That will disable the button, if needed

Upvotes: 7

aioobe
aioobe

Reputation: 420991

Is it possible or do i have to remove and than add them actionListener again.

You can add a boolean variable in the action listener like this:

boolean ignoreEvents = false;

and then wrap your action-code in

if (ignoreEvents)
    return;

ignoreEvents = true;

// your code here...

ignoreEvents = false;

Just make sure the code is properly synchronized.


From a user-interface perspective, you might be better of disabling the buttons instead.

Upvotes: 2

Related Questions