keyboard shortcut for a button using java

Here it show how to install a keyboard shortcut to a Swing JButton so the button behaves as though it was clicked when the key is pressed.

JButton jButton1 = new JButton();

int key=2 ;
//key=1->shift, key =2 -> ctrl
InputMap inputMap = jButton1.getInputMap(JButton.WHEN_IN_FOCUSED_WINDOW);
KeyStroke sav = KeyStroke.getKeyStroke(KeyEvent.VK_S, key);
inputMap.put(sav, "Save");
jButton1.getActionMap().put("Save", new ClickAction(jButton1));

jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
System.out.println("Save Operation");


class ClickAction1 extends AbstractAction {
private JButton button;

public ClickAction1(JButton button) {
this.button = button;

public void actionPerformed(ActionEvent e) {