File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes JTextField highlighted on windows Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTextField highlighted on windows " Watch "JTextField highlighted on windows " New topic

JTextField highlighted on windows

Larry Frissell
Ranch Hand

Joined: May 16, 2008
Posts: 82
On my program I am setting the UIManager to CrossPlatformLookAndFeel. I am running the program on both a Mac and Windows. On the mac as I tab through my JTextFields the text is highlighted and the default text is erased when the user begans typing. On the Windows system the text is not highlighted and the cursor appears at the end of the default text. I would like the Windows system to highlight the text also, I assume I need to change the a property on the UIDefaults, is that correct and which value needs to be changed.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10828

Can't find anything in UIDefaults. However, long time back, when I wanted the same behavior on Windoze, I had added subclassed JTextField, added a focus listener, and on focus gained, selected the existing text.

[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
if you have multiple textfields, perhaps a global focus listener might suit

and if you want the caret position at the beginning, change this

Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2454
If you only want this behaviour for specific text fields then you would use the FocusListener approach as suggested by Maneesh.

If you want this on all text fields, then I would use a KeyboardFocusManager. The example found in Global Event Listeners will work for formatted text fields as well.
Rob Spoor

Joined: Oct 27, 2005
Posts: 20276

Michael Dunn wrote:

Would this be easier:
Larry Frissell
Ranch Hand

Joined: May 16, 2008
Posts: 82
Thank you everyone, it is working perfectly
I agree. Here's the link:
subject: JTextField highlighted on windows
It's not a secret anymore!