Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Retaining Focus in Swing

 
Ravi Kanth D
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Upon validating a JTextField value on the focusLost event, how do I retain the focus back on the same JTextField until correct value is entered? Thanks.
Ravi.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should be able to call requestFocus() on the JTextField.
 
Ravi Kanth D
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried but doesn' work. This is what I am doing...please let me know if I am wrong:
In focusLost event, I said if length() is < 4 then show an error message on the bottom text area...and requestFocus()
When I do not enter a value in first control and click on the second control...it does display the error message correctly...but cursor is still in the second control... I want it to come back to first control. How do I do it? Thanks.
Ravi.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll have to excuse the horrible looking GUI here, but this works for me.
 
Ravi Kanth D
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Earlier I did something stupid...so it didn' work. Thx for your help again.
Ravi.
 
Garandi Garandi
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try inputListener

I hope this will help
Thank you
Garandi
 
VIJAY Yadlapati
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try InputVerifier.
see InputVerifier. There is an example also..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic