Search code examples
javaswingjtextfieldkeylistenerjmenubar

Adding JTextField to JMenuBar cancels KeyListener responses!! - How to fix?


The following is an SSCCE code; it works OK and the KeyListener responds correctly, but adding a JTextField to the JMenuBar causes the KeyListener to not respond at all.

Go down to uncomment/comment the line that adds a JTextField to see the difference: menuBar.add(textField);

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class JMenueWithKeyListenerExample extends JPanel {

    JTextField textField;
    JMenuBar menuBar;
    JFrame f;

    public JMenueWithKeyListenerExample() {

        textField = new JTextField();

        menuBar = new JMenuBar();
        JMenu fileMenue = new JMenu("File");
        JMenuItem menuItem1 = new JMenuItem("Item 1");
        JMenuItem menuItem2 = new JMenuItem("Item 2");

        fileMenue.add(menuItem1);
        fileMenue.add(menuItem2);

        menuBar.add(fileMenue);
      //menuBar.add(textField); // switch between comment & uncomment to see the difference

        f = new JFrame();
        f.setJMenuBar(menuBar);
        f.add(this);

        f.addKeyListener(new KeyAdapter() {

            @Override
            public void keyPressed(KeyEvent e) {
                if ((e.getKeyCode() == KeyEvent.VK_Z) && ((e.getModifiers() & KeyEvent.CTRL_MASK) != 0)) {
                    System.out.println("undo");
                }
            }
        });

        f.setSize(400, 400);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new JMenueWithKeyListenerExample();
            }
        });
    }
}

Solution

  • The preferred way to add shortcut keys is to use input map and action map, like this:

    f.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
            .put(KeyStroke.getKeyStroke(KeyEvent.VK_Z, KeyEvent.CTRL_DOWN_MASK),
                 "Undo");
    f.getRootPane().getActionMap()
            .put("Undo", new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("undo");
        }
    });
    

    Of course, in real code you should define a constant instead of hard-coding the "Undo" action name directly. And maybe define the action separately, giving it a proper name and other useful properties.