• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Losing focus callback - JTextField ?

 
zoe goddard
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Zoe,
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...
HTH,
-Nate
 
zoe goddard
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks,
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
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic