• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is there a way to make a component lose focus?  RSS feed

 
Yuriy Zilbergleyt
Ranch Hand
Posts: 429
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a JTextField that I want to lose focus when the Enter key inside it is hit. Is there some way I can make it lose focus, probably from the field's listener's actionPerformed() method, without actually assigning focus to another element?

Thank you,
Yuriy
 
Yuriy Zilbergleyt
Ranch Hand
Posts: 429
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, the only way of doing this that I've found so far, is to call "requestFocus()" on a component that doesn't care about focus = a JLabel or a "RigidArea" for example. Does anyone know of a less hackish solution?

Thank you,
Yuriy
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would say the default behaviour would be for something to have focus.

on [ENTER] you could make the caret invisible (if that's what you're trying to do)
and in the textfield's focusLost() restore the caret, but this is even more
hackish than your current solution
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!