简体   繁体   English

摆动:将功能键(F2)设置为加速器

[英]Swing: Setting a function key (F2) as an accelerator

I have a menu item, "rename", for which F2 is set as an accelerator. 我有一个菜单项“重命名”,F2被设置为加速器。 Indeed when the menu is displayed there a little "F2" indication next to "rename". 实际上,当菜单显示时,“重命名”旁边有一点“F2”指示。

Sadly, this does not work. 可悲的是,这不起作用。 This accelerator triggers no response. 此加速器不会触发响应。 When I change the accelerator to CTRL+F2 - it works. 当我将加速器更改为CTRL + F2时 - 它可以工作。

It seems that I should use an InpoutMpa/ActionMap. 看来我应该使用InpoutMpa / ActionMap。 The problem with that is that I want this to work everywhere in the app so I am trying to associate it with the top-level JFrame. 问题是我希望这在应用程序的任何地方都可以工作,所以我试图将它与顶级JFrame相关联。 But, JFrame does not have a getInputMap() method. 但是,JFrame没有getInputMap()方法。

Lost. 丢失。

[Added] [添加]

     ks = KeyStroke.getKeyStroke(KeyEvent.VK_F2, 0);
     JMenuItem mi = new JMenuItem("Rename");
     mi.setAccelerator(ks);
     mi.addActionListener(action); 

I know this is an old thread, but I struggled with the exact same thing as the original poster and found the solution. 我知道这是一个旧线程,但我与原始海报完全相同,并找到了解决方案。 The JFrame itself doesn't have a getInputMap method, but its root pane does. JFrame本身没有getInputMap方法,但它的根窗格却有。 So you have to use "getRootPane.getInputMap" instead. 所以你必须使用“getRootPane.getInputMap”。

Example code: 示例代码:

public class ApplicationFrame extends JFrame {
    private AbstractAction f2Action = new AbstractAction() {
        private static final long serialVersionUID = 1L;

        public void actionPerformed(ActionEvent e) {
            // Do something useful
        }
    };

    public ApplicationFrame() {

        InputMap inputMap = getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
        ActionMap actionMap = getRootPane().getActionMap(); 

        inputMap.put(KeyStroke.getKeyStroke("F2"), "f2Action");
        actionMap.put("f2Action", f2Action);

    }
}

This is probably happening because JTable uses F2 to invoke the StartEditing action (I saw the same behavior on one of my programs and traced it to this). 这可能正在发生,因为JTable使用F2来调用StartEditing操作(我在其中一个程序上看到了相同的行为并将其跟踪到此)。

There are a couple of solutions. 有几种解决方案。 The most drastic is to remove this input mapping (I believe this code actually removes the mapping from all JTables): 最激烈的是删除这个输入映射(我相信这段代码实际上删除了所有JTable的映射):

KeyStroke keyToRemove = KeyStroke.getKeyStroke(KeyEvent.VK_F2, 0);

InputMap imap = table.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
while (imap != null)
{
    imap.remove(keyToRemove);
    imap = imap.getParent();
}

Or, if you're using the table for display only, and don't plan to let the user edit it, you could make it non-focusable: 或者,如果您仅使用该表进行显示,并且不打算让用户对其进行编辑,则可以使其不可聚焦:

table.setFocusable(false);

On a completely different subject, I strongly recommend creating an AbstractAction subclass for your menu items, rather than creating them "from scratch". 在一个完全不同的主题上,我强烈建议为菜单项创建一个AbstractAction子类,而不是“从头开始”创建它们。 Aside from giving you very simple menu setup code, you can use the same action instance for both the main menu and a popup/toolbar, and enable/disable them both at the same time. 除了为您提供非常简单的菜单设置代码外,您还可以对主菜单和弹出/工具栏使用相同的操作实例,并同时启用/禁用它们。

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

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