I have a question regarding JTextField (java7). I did a search and saw there were over 4000 posts that refer to JTextField and realized quickly that I can't read them all to see if my question has been answered already. I scanned some of them but didn't find it to be so.
I know it's possible to define a JTextField so that it appears in the GUI with text already in it. Easy enough. I want to do this so that what appears there is an example (not a tooltip, I know how to do that but that's not what I want) of what a user might enter. If the field was to receive a number, for example, then I'd want to have 'i.e. 0.0' appear to show that the field accepts a double (the contents of the fileld will be parsed later of course). Still, easy enough. This requires the user to delete the text that pre-fills the field before they can enter their own. I swear, though, I saw somewhere that you can do all this so that, when the user clicks on the field to enter their own, the text that is initially there instantly vanishes so that they just start entering their own and don't have to specifically delete the pre-existing text. I can't see this in the API and can't even think what this might be called (if I didn't just dream it) in order to search for it.
I'd appreciate any tip on where to find this in the API (if, like I said, it exists and I didn't just dream it).
I think the easiest way to do that is to have the entire contents of the JTextField become selected when the field gets the focus. That way, as soon as the user types anything, the selection would be replaced by what they typed.
> I take it I must have dreamed it being a typical JTextField function, then.
it's not in-built, but a common function e.g. open a JFileChooser with a selected file set,
upon opening, that filename will be highlighted and a single keystroke will remove all that was there.
from Paul's post the keywords are underlined
"JTextField become selected when the field gets the focus."
basically, the right component with the right listener, to do the right action,
and you'll have what you seek.
(for the action/method you may need to dig into the inherited methods)
Joined: Aug 04, 2012
I looked at the TextPrompt class, Darryl. It appears to do what I was wanting to do. Interesting. Thanks.
I looked into figuring out how to accomplish it myself, with my own code though - which is the way I'd prefer - and adding a focus listening method, and some inheritted methods, was the way to go. I have to look more carefully in the future because what I wanted was 'buried' as inherited methods, as hinted at by Michael.
As well as selecting the entire field when the focus is on the text field component (thanks Paul), I found I could replace it with an empty String. This works just as well. I can see some good uses for selecting in the future though so that will come in handy.
Thanks for the help. I learned a lot just from this one little endeavour.
subject: JTextField pre-fill the field with text that instantly dissapears when user enters theirs