Niklas P
Niklas P

Reputation: 3507

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:

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.

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.

It works with the KeyStroke Ctrl+U but not with 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.

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.


Here's a small code for a program that does not react on the KeyStroke Ctrl+C, which is connected to a menu item:

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

}

Upvotes: 0

Views: 777

Answers (2)

panounou
panounou

Reputation: 11

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.

I solve my problem by calling requestFocusInWindow for the JMenuBar containing the items each time setEnable (true) is called.

Seems that selecting something in the table directs Ctrl+C / Ctrl+x to the table...

May be it'll help

Upvotes: 1

Anto
Anto

Reputation: 4305

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

Upvotes: 0

Related Questions