I have a TextField that is used for search. And it needs to have gray text like "Print search text here" when the field is empty and doesn't contain cursor. How can this be made?
Asked
Active
Viewed 6,399 times
2 Answers
1
Can't you just use a FocusListener that sets the text and the foreground color when the JTextField gains or loses its focus?
Your JTextField would contain that text and have a gray foreground until it gains focus, at which point you clear the text and set the foreground to black. When the focus is lost, if the JTextField is blank, you set the text back to the message and the foreground to gray.

Kevin Workman
- 41,537
- 9
- 68
- 107
1
Check out the Text Prompt class.
It allows you to display a prompt in the text field. You can control the transparency, color, font style, alignment etc.

camickr
- 321,443
- 19
- 166
- 288
-
I don't understand the down-vote; voting to close this question as a [duplicate](http://stackoverflow.com/q/20578568/230513); +1 to the citation [here](http://stackoverflow.com/a/20578858/230513); also endorsed by @kleopatra for "Elegance by simplicity." – trashgod Jan 29 '14 at 17:36