1

I have a program which I want to use keybindings:

// Imports..
public class Test{

JButton button = new JButton();

Test(){
//...
button.getInputMap().put(KeyStroke.getKeyStroke("A"), "A");
button.getActionMap().put("Action", action);
//...
}
}

Now how do I make the button respond when it is clicked?

Is it like KeyListeners where I have an actionPerformed method?

2 Answers 2

3

Now how do I make the button respond when it is clicked?

from KeyBindings is there only one way

button.doClick();

then this code line to invoke ActionListener or Swing Action added to the JButton

Sign up to request clarification or add additional context in comments.

Comments

3

Is it like KeyListeners where I have an actionPerformed method?

A KeyListener has no actionPerformed method! The solution is to add an ActionListener, or as @mKorbel points out, create the button using an Action.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.