I want to make a program with these goals:
1) Make a JButton 2) Attach the button to a key (The "A" Key) using KeyBindings 3) Execute some code when "A" is clicked
Here is the code I have so far:
// Imports
Public class Test{
JButton button = new JButton();
//...
Test(){
button.getInputMap().put(KeyStroke.getKeyStroke("A"), "Pressed");
//...
}
// Where do I add the code that responds when button is pressed?
}
Now where do I add the code that I want it to execute when the button is pressed?
Two ways I can think of:
doClick()
on the button from the key binding.KeyBindingEg.java
you need to add an action listener, specificaly for actionPerformed. declare this somewhere inside your constructor:
In this example if we press F2 it will be the equivalent of pressing the button.