Yes. Yet I am unable to do it!
I read many of us asking for this need, but no one of the answers seem
to solve the problem.
As a sample I have a frame with only a jTextField and two jButtons
(jButton1 and jButton2).
I would like to fire automatically the jButton2 hitting ENTER when I am
writing in the jTextField.
Can anyone write for me the code to do this?
Thank you. Franco.
ffellico@inwind.it wrote in news:1130566018.989557.54330
@g44g2000cwa.googlegroups.com:
> Yes. Yet I am unable to do it!
>
[quoted text clipped - 10 lines]
>
> Thank you. Franco.
Just add the same ActionListener to both. That way the same will happen
when you hit enter on the textfield or press the button. Use a different
listener (or a way to distinguish the source, eg event.getSource()) for the
second button.
ActionListener myActionListener = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
// ...
}
};
textField.addActionListener(myActionListener);
button.addActionListener(myActionListener);
button2.addActionListener(new ActionListener
{
// ...
});
ffellico@inwind.it - 29 Oct 2005 19:33 GMT
Hi.
You solved me also this problem ( before you suggest me the solution
for the statusbar at the bottom of a frame).
This is a new step for me in learning and using Java. Thank you.
Franco from Italy.
Babu Kalakrishnan - 30 Oct 2005 13:57 GMT
> You solved me also this problem ( before you suggest me the solution
> for the statusbar at the bottom of a frame).
>
> This is a new step for me in learning and using Java. Thank you.
> Franco from Italy.
A more generic solution for this is to use the "defaultButton" property
of the rootPane on the Window/Frame/Dialog. See the API docs for the
setDefaultButton method in the class javax.swing.JRootPane.
BK