Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / September 2005

Tip: Looking for answers? Try searching our database.

Ditching focus of a JTextField

Thread view: 
Rob McDonald - 27 Sep 2005 15:27 GMT
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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.