简体   繁体   English

将KeyStroke添加到JCheckBox

[英]Adding KeyStroke to JCheckBox

I want to add KeyStrokes to group of CheckBoxes ,so when user hits 1, keystroke will selected / deselected first JCheckBox. 我想将KeyStrokes添加到CheckBoxes组,因此当用户点击1时,击键将首先选择/取消选择JCheckBox。

I have made this part of code ,but its not working, can somebody point me into correct direction? 我已经编写了这部分代码,但是它不起作用,有人可以向我指出正确的方向吗?

    for (int i=1;i<11;i++)
     {
           boxy[i]=new JCheckBox();
           boxy[i].getInputMap().put(KeyStroke.getKeyStroke((char) i),("key_"+i));  
           boxy[i].getActionMap().put(("key_"+i), new AbstractAction() {  
                 public void actionPerformed(ActionEvent e) {  
                     JCheckBox checkBox = (JCheckBox)e.getSource();  
                     checkBox.setSelected(!checkBox.isSelected());  
         }});
          pnlOdpovede.add(boxy[i]);
       }

The problem is that you registered the bindings with the checkBox' inputMap of type WHEN_FOCUSED: they will be effective only for that particular checkBox that is focused at the time of the keyPressed. 问题是您用WHEN_FOCUSED类型的checkBox的inputMap注册了绑定:它们仅对在keyPressed时聚焦的特定checkBox有效。

Assuming that you want to toggle the selected state independent of the focusOwner, an alternative is to register the keyBindings with the parent container of the checkBoxes and add some logic to find the component that's meant to have its selection state toggled: 假设您要独立于focusOwner来切换选定状态,另一种方法是在checkBoxes的父容器中注册keyBindings,并添加一些逻辑来查找要切换其选择状态的组件:

// a custom action doing the toggle
public static class ToggleSelection extends AbstractAction {

    public ToggleSelection(String id) {
        putValue(ACTION_COMMAND_KEY, id);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Container parent = (Container) e.getSource();
        AbstractButton child = findButton(parent);
        if (child != null) {
            child.setSelected(!child.isSelected());
        }
    }

    private AbstractButton findButton(Container parent) {
        String childId = (String) getValue(ACTION_COMMAND_KEY);
        for (int i = 0; i < parent.getComponentCount(); i++) {
            Component child = parent.getComponent(i);
            if (child instanceof AbstractButton && childId.equals(child.getName())) {
                return (AbstractButton) child;
            }
        }
        return null;
    }

}

// register with the checkbox' parent
for (int i=1;i<11;i++)  {
       String id = "key_" + i;
       boxy[i]=new JCheckBox();
       boxy[i].setName(id);
       pnlOdpovede.getInputMap(WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
           .put(KeyStroke.getKeyStroke((char) i), id);  
       pnlOdpovede.getActionMap().put(id, new ToggleSelection(id));
       pnlOdpovede.add(boxy[i]);
 }

BTW: assuming your checkBoxes have Actions (which they should :-), the ToggleAction could trigger those Actions instead of toggling the selection manually. 顺便说一句:假设您的复选框具有Actions(它们应该是:-),则ToggleAction可以触发这些Actions,而不是手动切换选择。 This approach is used in a recent thread 在最近的线程中使用了这种方法

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

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