Raceimaztion
Raceimaztion

Reputation: 9624

Java: Disabling an Action should disable JButtons and JMenuItems

I'm writing a fairly simple IDE for developing embedded programs (for iRobot's Create platform) and almost every single button and menu item is backed by Java's Action system. This has made it easier to handle all the operations that the user will want without duplicating an operation's trigger.

What I would like to know is, how do I disable the JButtons and JMenuItems created from an Action by disabling the Action itself?

In case it helps, I've written an Action-wrapping class that allows me to easily create a JButton or JMenuItem straight from the Action itself, which means I have hooks in place already to add stuff to the buttons or menu items should the need arise.

Any suggestions?

Upvotes: 0

Views: 2016

Answers (2)

bobndrew
bobndrew

Reputation: 417

Short answer:
anAction.setEnabled( false );

Shorter answer:
http://sscce.org/

Upvotes: 3

Mikita Belahlazau
Mikita Belahlazau

Reputation: 15434

You can store all buttons and menuitems to List<AbstractButton> buttons and add listener to action:

action.addPropertyChangeListener(new PropertyChangeListener() {
   public void propertyChange(PropertyChangeEvent evt) {
      if (evt.getPropertyName().equals("enabled")) {
         boolean isEnabled = (Boolean)evt.getNewValue();
         for (AbstractButton button : buttons) {
            button.setEnabled(isEnabled);
         }
      }
   }
});

Upvotes: 0

Related Questions