| Author |
2 buttons listening for the same key
|
Jody Vandy
Greenhorn
Joined: Nov 13, 2003
Posts: 3
|
|
I have two forms in my frame, each with their own submit button. When a user presses the return key on their keyboard, I would like to doCick() on the button which the user last made changes to the form fields for that respective button. Any ideas? I think I need to do something like this but I'm not sure how: if a text field is modified, that form's button comes to focus. Then, when enter is pressed, the button which is in focus is clicked. Thanks!
|
 |
Daniel Puryear
Greenhorn
Joined: Jun 02, 2002
Posts: 22
|
|
Keystrokes are normally delivered to the object with the "input" focus, e.g. your JTextField undergoing edit. Any object can listen in on the Keyboard by registerning as a listener. Its generally easier to start by extending the KeyAdapter class, such as .... //inner class >> class myKeyAdapter extends java.awt.event.KeyAdapter { public void keyPressed(java.awt.event.KeyEvent event) { //jump back out to your base class keyPressActions(event); } } //in your base class, put the "do it" code protected void keyPressActions(java.awt.event.KeyEvent event) { int keycode = event.getKeyCode(); Object object = event.getSource(); if (object instanceof ValueLabel){ if (keycode == java.awt.event.KeyEvent.VK_UP ){ ValueLabel vl =(ValueLabel)getPreviousTarget(((ValueLabel)object).getProperty()); vl.requestFocus(); } else if (keycode == java.awt.event.KeyEvent.VK_DOWN ){ ValueLabel vl =(ValueLabel)getNextTarget(((ValueLabel)object).getProperty()); vl.requestFocus(); } else if (keycode == java.awt.event.KeyEvent.VK_ENTER ){ ValueLabel vl = (ValueLabel)object; showEditor(vl); } } else if (object instanceof IPropertyEditor) { if (keycode == java.awt.event.KeyEvent.VK_ENTER || keycode == java.awt.event.KeyEvent.VK_TAB) { retireEditor((IPropertyEditor)object); } } event.consume(); } then register that adapter in any class you want to actually processs the KeyPress events... addKeyListener(myKeyAdapter ); you could add it to each of the editor panels, but from description, I'd probably add it to their container and callback on each on the buttons action code. Hope this helps..
|
 |
Nathan Pruett
Bartender
Joined: Oct 18, 2000
Posts: 4121
|
|
"Jody" - Welcome to the JavaRanch! Please adjust your displayed name to meet the JavaRanch Naming Policy. You can change it here. Thanks! and welcome to the JavaRanch!
|
-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
|
 |
Jody Vandy
Greenhorn
Joined: Nov 13, 2003
Posts: 3
|
|
Hi- Thanks for the reply. I'm fairly new to swing so there are a couple things about your code that I need clarification on. I get the general idea of what your trying to do but I'm confused about the code in keyPressedActions. ValueLabel? getPreviousTarget? getNextTarget? IPropertyEditor? Can you please elaborate? Thanks!
|
 |
Nathan Pruett
Bartender
Joined: Oct 18, 2000
Posts: 4121
|
|
|
You're probably not going to want to set up a key listener on a field to call the onClick() method of the button. JTextField can have an ActionListener that gets called when 'Enter' is pressed in it already. And JButton can have an ActionListener that gets called when the button is pressed. What you are going to want to do is to make sure that the same listener gets called when either of these events happen. You can do that by passing the same ActionListener reference to both the JTextField and the JButton.
|
 |
 |
|
|
subject: 2 buttons listening for the same key
|
|
|