0

For some unknown reason, this code doesn't work.

JTextComponent component = new JTextArea();
Keymap keymap = component.getKeymap();
keymap.addActionForKeyStroke(KeyStroke.getKeyStroke(KeyEvent.VK_1, InputEvent.ALT_DOWN_MASK), myAction);

It never triggers myAction when I press Alt and 1 (not on the numpad, the 1 above the letters). This also does not work for any modifier.

The solution given here https://stackoverflow.com/a/11974908/3838784 does not work, the action is still not triggered. Removing all keystrokes from the key map that have ALT as a modifier does not seem to help either.

Community
  • 1
  • 1

1 Answers1

1

I tried a sample, using your code, it seems to be working fine. Can you let me know if anything is different from your code?

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import javax.swing.*;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.text.JTextComponent;
import javax.swing.text.Keymap;

public class MyExample extends JFrame {

    MyExample() {
        initGUI();
    } // Constructor

    private void initGUI() {
        // Create the button
        JButton myButton = new JButton("Press ME !");

        // Create the label
        JLabel testLabel = new JLabel("TEST !!!");

        // Set up the window 
        JPanel pane = (JPanel) getContentPane();
        pane.setLayout(new BorderLayout());

        pane.add(myButton, BorderLayout.CENTER);
        pane.add(testLabel, BorderLayout.SOUTH);
        JTextComponent component = new JTextArea();
        pane.add(component, BorderLayout.SOUTH);
        Keymap keymap = component.getKeymap();

        keymap.addActionForKeyStroke(KeyStroke.getKeyStroke(KeyEvent.VK_1, InputEvent.ALT_DOWN_MASK), new MyAction());

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        pack();
        // setSize(600,300);  

    }

    public static void main(String[] args) {
        try {
            // Set System L&F
            for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (UnsupportedLookAndFeelException e) {
            // handle exception
        } catch (ClassNotFoundException e) {
            // handle exception
        } catch (InstantiationException e) {
            // handle exception
        } catch (IllegalAccessException e) {
            // handle exception
        }
        // new MyExample().setVisible(true);
        EventQueue.invokeLater(() -> {
            MyExample me = new MyExample();
            me.setVisible(true);
        });
    }

    class MyAction extends AbstractAction{

        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("Action performed");
        }

    }
}
Beniton Fernando
  • 1,533
  • 1
  • 14
  • 21
  • Your sample is not working for me. This must be a bug on Ubuntu MATE or something, both OpenJDK and Oracle JDK do not work when running this test... :( –  Jun 15 '16 at 17:35
  • yep i might be. I m not using ubuntu, so it would bot possible for me to check. But this works in Windows. – Beniton Fernando Jun 16 '16 at 02:06