Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

2 buttons listening for the same key  RSS feed

 
Jody Vandy
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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!
 
Jody Vandy
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!