I am trying to get a simple KeyBindings program to work. I followed the instructions from java doc, and tried to test the stuff answered in this (Java Key Bindings Not Working) thread, but i just cant get it to work. I want to output "test" to the console when "F1" is pressed. Can anyone spot my mistake?
JFrame frame = new JFrame("shit");
frame.setSize(800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
JPanel jPanel = new JPanel();
jPanel.getInputMap().put(KeyStroke.getKeyStroke("F1"), "focus");
jPanel.getActionMap().put("focus", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("test");
}
});
frame.add(jPanel);