I need to trigger a Button press when the operator has entered data in a
JTextField and presses the Enter key on the keyboard.
I set up an KeyListener for my JTextField for capturing the "Enter" key but
what do I call to trigger a button press?
My work around currently, is to move all of my code from under the button's
actionPerformed method to a separate method then call that method from the
KeyListener. But I was hoping I could just trigger the button press
(actionPerformed method for the button) when the operator presses the Enter
key when the cursor is in the text field.
myTextField.addKeyListener(new KeyListener)
{
public void keyPressed (KeyEvent e)
{
if (e.getKeyCode() == java.awt.event.KeyEvent.VK_ENTER)
{
// trigger myButton here (but how?)
}
}
}
...
...
...
private void myButton(java.awt.event.ActionEvent evt)
{
// do a bunch of cool stuff here
}
Thanks in advance.
Vova Reznik - 22 Nov 2005 20:40 GMT
> I need to trigger a Button press when the operator has entered data in a
> JTextField and presses the Enter key on the keyboard.
[quoted text clipped - 7 lines]
> (actionPerformed method for the button) when the operator presses the Enter
> key when the cursor is in the text field.
> myTextField.addKeyListener(new KeyListener)
> {
[quoted text clipped - 6 lines]
> }
> }
change to
myTextField.addActionListener(yourButtonListener);
> ...
> ...
[quoted text clipped - 7 lines]
>
> Thanks in advance.
Oliver Wong - 22 Nov 2005 20:48 GMT
>I need to trigger a Button press when the operator has entered data in a
>JTextField and presses the Enter key on the keyboard.
>
> I set up an KeyListener for my JTextField for capturing the "Enter" key
> but what do I call to trigger a button press?
You might want to look at javax.swing.AbstractionAction in particular,
and the Command Pattern in general. Conceptually, you are not so much
interested in causing a button to be clicked when the user presses enter;
more likely, you want the action associated with that button to execute when
the user presses enter.
By using AbstractAction, you write the code in one location, and can
have multiple GUI widgets refer to the same action (e.g. buttons, menus,
keypresses, etc.).
- Oliver
decalod85 - 22 Nov 2005 21:42 GMT
> I need to trigger a Button press when the operator has entered data in a
> JTextField and presses the Enter key on the keyboard.
Get the rootpane of the dialog, and set the button you want pressed as
the default Button.
dialog.getRootPane().setDefaultButton(button);
Won't work well if a JTable has focus, but gets the job done.
Corrado - 23 Nov 2005 10:46 GMT
> I need to trigger a Button press when the operator has entered data in a
> JTextField and presses the Enter key on the keyboard.
[quoted text clipped - 29 lines]
>
> Thanks in advance.
I think there are a lot of ways.
The simplest one, is to ose the method doClick() (defined in
AbstractButton, maybe this is why you didn't see it in the javadocs).
Or instead of adding a KeyListener...
public class MyFrame extends JFrame implements ActionListener {
JButton button;
public MyFrame() {
JTextField text = new JTextField();
text.addActionListener(this);
getContentPane().add(text);
button = new JButton("Ok");
button.setActionCommand("ok");
button.addActionListener(this);
getContentPane().add(button);
//Make visible and other stuff...
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().compareTo("ok") == 0) {
//When user press the OK button
} else {
//When user press enter on the TextField
actionPerformed(new ActionEvent(this, 0, "ok"));
//Or here you could use 'button.doClick();'
}
}
}
Hope I've helped you.
Bye
Corrado