I have implemented a simple JTextField in my application. I am using an
ActionListener to perform an action when the user presses enter after
completing input. This works fine....
However, after pressing enter, the cursor remains blinking in the box. The
user has no feedback that his input has been accepted. I need to force the
focus away from the textfield after input is complete. However, I don't
really want focus to go anywhere else in particular....
Also, is there an event that triggers when a user manually changes focus off
of the text box? I would like my action to occur any time the user changes
the value (once they're done typing and has changed focus by clicking on
something else), not just when the user presses enter.
Thanks in advance,
Rob
Vova Reznik - 27 Sep 2005 16:14 GMT
java.awt.Component
protected void processFocusEvent(FocusEvent e)
...
// --- after input is complete ---
super.processFocusEvent(new FocusEvent(this, FocusEvent.FOCUS_LOST));
this - your textField.
You should subclass JTextField to have access to that method.
> I have implemented a simple JTextField in my application. I am using an
> ActionListener to perform an action when the user presses enter after
[quoted text clipped - 13 lines]
>
> Rob
Monique Y. Mudama - 27 Sep 2005 16:57 GMT
> I have implemented a simple JTextField in my application. I am
> using an ActionListener to perform an action when the user presses
[quoted text clipped - 5 lines]
> complete. However, I don't really want focus to go anywhere else in
> particular....
I've actually approached this differently. If you have a JButton that
also performs the same function as hitting enter, you can have your
JTextField respond to an enter by calling the JButton's doClick()
method. This way the button will actually "depress" and give a visual
indicator that something's going on. No need to mess with focus.
> Also, is there an event that triggers when a user manually changes
> focus off of the text box? I would like my action to occur any time
[quoted text clipped - 5 lines]
>
> Rob

Signature
monique
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
Roedy Green - 27 Sep 2005 19:14 GMT
>However, after pressing enter, the cursor remains blinking in the box. The
>user has no feedback that his input has been accepted. I need to force the
>focus away from the textfield after input is complete. However, I don't
>really want focus to go anywhere else in particular....
What happens if you do a setFocus( null )?
this is not the solution to you problem, but to a closely related one.
So I bring it up.
theButton.setFocusPainted( false );

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
Rob McDonald - 27 Sep 2005 21:07 GMT
The answer for me was two parts.
1) make the ActionEvent do a "mainPane.requestFocus();" mainPane is my
all-encompasing JPanel. When it gets focus, you can't really tell it has
it, so that is good enough for me.
2) create a FocusListener where the focusLost method does all the real work.
It gets called when the user clicks off somewhere else, and also when the
mainPane takes focus as directed in the ActionEvent.
Thanks for all the help,
Rob
> I have implemented a simple JTextField in my application. I am using an
> ActionListener to perform an action when the user presses enter after
[quoted text clipped - 9 lines]
> the value (once they're done typing and has changed focus by clicking on
> something else), not just when the user presses enter.