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 ]
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?
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop