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 ea开发者_StackOverflowsier 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?
Short answer:
anAction.setEnabled( false );
Shorter answer:
http://sscce.org/
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);
}
}
}
});
精彩评论