简体   繁体   English

禁用JMenuItem后KeyStroke加速器不起作用

[英]KeyStroke Accelerator not working after disabling JMenuItem

Within a small Java standalone program with a Swing GUI I use JMenuItem with Accelerator - mostly without any problems: 在带有Swing GUI的小型Java独立程序中,我将JMenuItem与Accelerator结合使用-基本上没有任何问题:

JMenuItem item = new JMenuItem("Connect", 'C');
KeyStroke ks = KeyStroke.getKeyStroke('C', Event.CTRL_MASK);
item.setAccelerator(ks);
item.addActionListener(this);

My problem is that when I disable the item with 我的问题是,当我禁用与项目

item.setEnabled(false);  

and enable it later with 稍后再启用

item.setEnabled(true);

I cannot use the accelerator any more. 我不能再使用加速器了。 The JMenuItem is correctly shown as enabled in the menu and I can click it with the mouse (and my ActionListener is correctly working) but my accelerator isn't working - so I cannot start "Connect" with Ctrl+C any more. JMenuItem在菜单中正确显示为已启用,我可以用鼠标单击它(并且我的ActionListener正常工作),但是我的加速器不起作用-因此我无法再使用Ctrl + C启动“连接”。

Does anyone of you know what this problem is or how I can avoid it? 你们当中有人知道这个问题是什么或者我如何避免它?


Other menu items which accelerators (but without beeing temporarily disabled) are working. 加速器(但没有暂时禁用蜂鸣器)正在工作的其他菜单项。 When calling 打电话时

item.getAccelerator();

after calling item.setEnabled(true) I get the formerly set KeyStroke. 调用item.setEnabled(true)之后,我得到了以前设置的KeyStroke。

It works with the KeyStroke Ctrl+U but not with Ctrl+C. 它适用于KeyStroke Ctrl + U,但不适用于Ctrl + C。 It seems to me that when disabling the menu item the default copy operation is registered again with Ctrl+C and after enabling the menu item again there's no connection between the KeyStroke and the menu item any more. 在我看来,当禁用菜单项时,默认的复制操作将再次用Ctrl + C注册,并且在再次启用菜单项之后,KeyStroke和菜单项之间不再存在连接。

While trying to build a small copy of my program to demonstrate the problem I got it: 尝试构建我的程序的小副本以演示问题时,我明白了:
I did two things together - enabled the JMenuItem (with KeyStroke Ctrl+C) AND requested focus for a JTextField. 我一起做了两件事-启用JMenuItem(使用KeyStroke Ctrl + C),并请求对JTextField进行聚焦。


Here's a small code for a program that does not react on the KeyStroke Ctrl+C, which is connected to a menu item: 这是程序的一小段代码,它对与菜单项相连的KeyStroke Ctrl + C不起作用:

public class ProblemDemo extends JFrame implements ActionListener {

public ProblemDemo() {
    super("ProblemDemo");
    setSize(500,500);
    setLocation(500,300);

    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    JMenuBar menubar = new JMenuBar();
    JMenuItem menuItem = new JMenuItem("JMenuItem", 'C');
    menuItem.setAccelerator(KeyStroke.getKeyStroke('C', Event.CTRL_MASK));
    menuItem.addActionListener(this);
    JMenu menu = new JMenu("Actions");
    menu.add(menuItem);
    menubar.add(menu);
    setJMenuBar(menubar);

    JTextArea textarea = new JTextArea();
    getContentPane().setLayout(new BorderLayout());
    getContentPane().add(textarea, BorderLayout.CENTER);

    setVisible(true);
}

@Override
public void actionPerformed(ActionEvent event) {
    if ("JMenuItem".equals(event.getActionCommand())) {
        System.out.println("JMenuItem clicked");
    }
}

public static void main(String[] args) {
    new ProblemDemo();
}

} }

I had the same problem trying to enable the copy/cut menu items (as well as buttons in a toolbar), only if something is selected in a JTable by calling setEnabled (true) in a ListSelectionListener. 只有在通过调用ListSelectionListener中的setEnabled(true)在JTable中选择了某些内容时,我才尝试启用复制/剪切菜单项(以及工具栏中的按钮)时遇到相同的问题。

I solve my problem by calling requestFocusInWindow for the JMenuBar containing the items each time setEnable (true) is called. 我通过为包含每次调用setEnable(true)的项目的JMenuBar调用requestFocusInWindow来解决我的问题。

Seems that selecting something in the table directs Ctrl+C / Ctrl+x to the table... 似乎选择表中的某些内容会将Ctrl + C / Ctrl + x定向到表...

May be it'll help 可能会有所帮助

Try to create the following method: 尝试创建以下方法:

private KeyStroke getNewKeyStroke(){

 KeyStroke ks = KeyStroke.getKeyStroke('C', Event.CTRL_MASK);
 return KeyStroke;

}   

then you can call the following method like that: 那么您可以像下面那样调用以下方法:

 item.setEnabled(true);
 item.setAccelerator(this.getNewKeyStroke());

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

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