Hi, this question is actually a bit simple but I want to hear from you folks what is the right way to do this. I have a JButton which has shortcut key using key bindings I was able to do this but I am confuse if I should use actionListener for mouse click.



What I did is add an actionListener and repeat the code I did in keybinding. Is this the right thing to do?
Java Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TryKeyBinding extends JFrame
{   private JPanel p;
    private JButton btn1;
    private JButton btn2;
    private JFrame main;
    public TryKeyBinding()
    {   //super();
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);

        main = this;
        p = new JPanel();
        btn1 = new JButton("CTRL + A");
        btn2 = new JButton("CTRL + L");

        p.setPreferredSize(new Dimension(200, 200));
        btn1.setPreferredSize(new Dimension(50, 50));
        btn2.setPreferredSize(new Dimension(50, 50));

        this.setLayout(new BorderLayout());
        this.add(p, BorderLayout.CENTER);

        p.setLayout(new GridLayout( 2 , 0 , 5 , 5 ));
        p.add(btn1);
        p.add(btn2);

        pack();

        btnActionListener();
        createKeyBind();
    }

    void btnActionListener()
    {   btn1.addActionListener(new ActionListener()
        {   public void actionPerformed(ActionEvent e)
            { JOptionPane.showMessageDialog(main, "'btn1' clicked.");
            }
        });

        btn2.addActionListener(new ActionListener()
        {   public void actionPerformed(ActionEvent ae)
            {   JOptionPane.showMessageDialog(main, "'btn2' clicked.");
            }
        });
    }
    void createKeyBind()
    {   ActionMap am_btn1 = btn1.getActionMap();
        am_btn1.put("action_btn1", new AbstractAction()
        {   public void actionPerformed(ActionEvent ae)
            {   JOptionPane.showMessageDialog(main, "'btn1' clicked.");
            }
        });

        InputMap im_btn1 = btn1.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
        im_btn1.put(KeyStroke.getKeyStroke( "control A" ), "action_btn1");

        btn2.getActionMap().put("action_btn2", new AbstractAction()
        {   public void actionPerformed(ActionEvent e)
            {   JOptionPane.showMessageDialog(main, "'btn2' clicked.");
            }
        });
        btn2.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("control L") , "action_btn2");
        btn2.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("alt L") , "action_btn2");

    }

    public static void main(String[] args)
    {   java.awt.EventQueue.invokeLater(new Runnable()
        {   public void run()
            {   TryKeyBinding tkb = new TryKeyBinding();
                tkb.setVisible(true);
            }
        });
    }
}
Hope I made my question clear.

Thanks,
geje