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