简体   繁体   English

Java:禁用操作应禁用JButtons和JMenuItems

[英]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. 我正在编写一个相当简单的IDE(用于iRobot的Create平台)来开发嵌入式程序,几乎每个按钮和菜单项都由Java的Action系统提供支持。 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? 我想知道的是,如何通过禁用Action本身来禁用从Action创建的JButton和JMenuItem?

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. 如果有帮助,我编写了一个Action-wrapping类,使我可以直接从Action本身轻松创建JButton或JMenuItem,这意味着我已经有了钩子,可以在需要时向按钮或菜单项添加内容出现。

Any suggestions? 有什么建议么?

Short answer: 简短答案:
anAction.setEnabled( false );

Shorter answer: 简短的答案:
http://sscce.org/ http://sscce.org/

You can store all buttons and menuitems to List<AbstractButton> buttons and add listener to action: 您可以将所有按钮和菜单项存储到List<AbstractButton> buttons ,并将侦听器添加到操作中:

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);
         }
      }
   }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM