Anonymous181
Anonymous181

Reputation: 1873

Getting Input using KeyBindings

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?

Upvotes: 1

Views: 104

Answers (2)

mKorbel
mKorbel

Reputation: 109823

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

Upvotes: 3

Andrew Thompson
Andrew Thompson

Reputation: 168845

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.

Upvotes: 3

Related Questions