Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

select text in jtextfield, but only when tabbing  RSS feed

 
tormod eriksen
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to select all the text in JTextFields, but only when they are tabbed to.
I know how to select text, but not how to do it only when they gain focus by a tab from another component.
 
Mahesh swami
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can add a focus listener to the text field.And override foucsGained(FocusEvent).Then you can write the code of selecting the text in this method
 
tormod eriksen
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried that, but haven't figured out how to check if it is tabbed to. Do you know how to do this?
 
Brian Duff
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is actually a bug in the Windows look and feel (it ought to behave like this by default)... Anyhoo, here's one way of solving it:
Rather than adding listeners directly to the field, instead change the caret implementation. The caret implementation is a mouse listener and a focus listener. You override the focus listening stuff to select all the text in the field, but in the mouse listening code, you save the current caret position before calling requestFocus() then reset the caret position after focus has been gained.
Something like this:

Should do the trick
Brian
[ August 23, 2002: Message edited by: Brian Duff ]
 
tormod eriksen
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx, that really helped!
After some various tests I found one unexpected behaviour. When I set focus (tab or with mouse) to a JTextField that has set this FixedCaret, and in this JTextField's focusGained method calls jtextfieldobject.requestFocus(), focus is set to the jtextfieldobject, but the caret / selection is still visible in the first of these JTextFields.
When I set focus to another component via the gui, with tab or mouse, the caret / selection in the JTextField mentioned is set invisible.
Any idea why this is as it is?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!