• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

select text in jtextfield, but only when tabbing

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
New rule: no elephants at the chess tournament. Tiny ads are still okay.
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic