Results 1 to 2 of 2
- 12-15-2007, 02:02 AM #1
Member
- Join Date
- Dec 2007
- Posts
- 1
- Rep Power
- 0
Assign a keyboard key to a JButton.
So, i need some help :)
I want to press a key, and invoke the action of a coresponding button.
So, for instance, if i press the key 1, i would like my jbutton (button[1]) to "be pressed"
Here is the keyboard scanning code i wrote
My class isJava Code:public void keyReleased(KeyEvent e){ } public void keyTyped(KeyEvent e){ } public void keyPressed (KeyEvent e){ keyID = e.getID(); if (keyID == 97 || keyID == 49){ // 1 } else if (keyID == 98 || keyID == 50){ // 2 } else if (keyID == 99 || keyID == 51){ // 3 } else if (keyID == 100 || keyID == 52){ // 4 } else if (keyID == 101 || keyID == 53){ // 5 } else if (keyID == 102 || keyID == 54){ // 6 } else if (keyID == 103 || keyID == 55){ // 7 } else if (keyID == 104 || keyID == 56){ // 8 } else if (keyID == 105 || keyID == 57){ // 9 } else if (keyID == 110 || keyID == 46){ // . } else if (keyID == 10){ // = } else if (keyID == 107){ // + } else if (keyID == 109){ // - } else if (keyID == 106){ // * } else if (keyID == 111){ // / } else if (keyID == 67){ // C } JOptionPane.showMessageDialog(null, keyID+" Was Pressed", "Exception Error", JOptionPane.INFORMATION_MESSAGE); }
So i haveJava Code:public class CalClass extends JFrame implements ActionListener, KeyListener {
But when i press a key i am not informed that a key was pressed by the JOptionPane, so i take im not doing something right.Java Code:this.addKeyListener(this);
ALSO, once i have it so where its listening for keypresses correctly, how can i get it to "Press" certin buttons for me?
For instance, where my code has //C I would like for it to execute the action that i execute when the JButton calculate = new JButton("C"); button is pressed....
Can anyone offer any guidance?
- 12-15-2007, 10:42 PM #2
Java Code:import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ButtonKeys { private JPanel getContent() { JPanel panel = new JPanel(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.weightx = 1.0; gbc.weighty = 1.0; for(int j = 0; j < 4; j++) { JButton button = new JButton("button " + (j+1)); button.setActionCommand(String.valueOf(j+1)); button.addActionListener(action); gbc.gridwidth = (j % 2 == 0) ? GridBagConstraints.RELATIVE : GridBagConstraints.REMAINDER; panel.add(button, gbc); } return panel; } private void registerKeys(JFrame f) { JRootPane rootPane = f.getRootPane(); int c = JComponent.WHEN_IN_FOCUSED_WINDOW; for(int j = 0; j < 4; j++) { String s = String.valueOf(j+1); rootPane.getInputMap(c).put(KeyStroke.getKeyStroke(s), s); rootPane.getActionMap().put(s, action); } } private Action action = new AbstractAction() { public void actionPerformed(ActionEvent e) { String ac = e.getActionCommand(); System.out.println("ac = " + ac); if(ac.equals("1")) System.out.println("do something for button 1"); } }; public static void main(String[] args) { ButtonKeys test = new ButtonKeys(); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().add(test.getContent()); f.setSize(400,400); f.setLocation(200,200); f.setVisible(true); test.registerKeys(f); } }
Similar Threads
-
getting each character from keyboard
By Sreejesh25 in forum New To JavaReplies: 11Last Post: 01-25-2011, 01:08 PM -
getting each character from keyboard
By Sreejesh25 in forum Advanced JavaReplies: 6Last Post: 03-05-2010, 07:12 PM -
Polled keyboard input through swing
By Prometheus in forum Advanced JavaReplies: 2Last Post: 02-04-2008, 04:05 PM -
Keyboard key are not working in solaris and linux.
By dinesh kaushik in forum AWT / SwingReplies: 1Last Post: 12-22-2007, 04:18 AM -
Help with keyboard events?
By Bibendum in forum New To JavaReplies: 2Last Post: 11-02-2007, 02:51 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks