Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-15-2007, 04:02 AM
Member
 
Join Date: Dec 2007
Posts: 1
gszauer is on a distinguished road
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
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); }
My class is
Code:
public class CalClass extends JFrame implements ActionListener, KeyListener {
So i have
Code:
this.addKeyListener(this);
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.

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?
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 12-16-2007, 12:42 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,266
hardwired is on a distinguished road
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); } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
getting each character from keyboard Sreejesh25 New To Java 8 04-08-2008 05:39 AM
getting each character from keyboard Sreejesh25 Advanced Java 1 04-02-2008 07:31 PM
Polled keyboard input through swing Prometheus Advanced Java 2 02-04-2008 06:05 PM
Keyboard key are not working in solaris and linux. dinesh kaushik AWT / Swing 1 12-22-2007 06:18 AM
Help with keyboard events? Bibendum New To Java 2 11-02-2007 04:51 AM


All times are GMT +3. The time now is 02:32 PM.


VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org