Results 1 to 1 of 1
Thread: JButton click
- 07-30-2010, 04:50 AM #1
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
JButton click
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?
Hope I made my question clear.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); } }); } }
Thanks,
geje
Similar Threads
-
Please click my links
By clydedoris in forum Reviews / AdvertisingReplies: 0Last Post: 07-19-2010, 11:42 AM -
JButton click problem
By mine0926 in forum NetBeansReplies: 7Last Post: 06-11-2010, 03:00 AM -
location of click
By saima in forum AWT / SwingReplies: 3Last Post: 11-22-2009, 12:06 PM -
i click on it,then first time error page comes,second time click then product page co
By 82rathi.angara in forum New To JavaReplies: 21Last Post: 08-01-2008, 11:13 AM -
how to click a jbutton and open an url
By katie in forum AWT / SwingReplies: 1Last Post: 08-06-2007, 10:44 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks