This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Losing focus callback - JTextField ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Losing focus callback - JTextField ?" Watch "Losing focus callback - JTextField ?" New topic

Losing focus callback - JTextField ?

zoe goddard
Ranch Hand

Joined: Dec 12, 2000
Posts: 74
I'm in the process of creating a JTextField that will only contain integers. I want to prevent only having a - sign and a null string. Is there a way in Java to be notified when the JTextField loses focus, so I can perform the last checks on the input data?
Nathan Pruett

Joined: Oct 18, 2000
Posts: 4121

Yes, what you would do is to add a FocusListener to the JTextField and use its focusLost( FocusEvent e ) method to handle this... Personally, I would either have it blank the field, or return focus to the field and pop up a warning message...

Write once, run anywhere, because there's nowhere to hide! - /. A.C.
zoe goddard
Ranch Hand

Joined: Dec 12, 2000
Posts: 74
Your suggestions are along the same lines that I was going to to do. I just want to force them to input a number, blanks for these fields don't make sense in my application.
Kris Schneider
Ranch Hand

Joined: Feb 14, 2001
Posts: 71
If you're using J2 v1.3, you might want to look into the javax.swing.JComponent.setInputVerifier(InputVerifier) method and the javax.swing.InputVerifier class.
jQuery in Action, 2nd edition
subject: Losing focus callback - JTextField ?
Similar Threads
Setting hot keys
RequestFocus doesn't work?
Retaining Focus in Swing
Text Cursor in a JTextField