8

I want to create a JTextField with a message inside as a deafult. But not as a proper text but as a comment about what to type inside the JTextField. So if i type jtf.getText() it returns null or empty because it is just a comment that was printed there. When you click on it then it disappears and you can write whatever you want on it. Is there any method to do such a thing?

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
Gustavo Baiocchi Costa
  • 1,379
  • 3
  • 16
  • 34
  • Take a look at the [Prompt support](http://weblogs.java.net/blog/kschaefe/archive/2010/07/15/swingx-using-promptsupport) in the latest SwingLabs, SwingX libraries – MadProgrammer Feb 16 '13 at 23:28
  • See also [`TextPrompt`](http://stackoverflow.com/q/1805486/230513). – trashgod Feb 16 '13 at 23:31
  • 1
    A more advanced and fancier version of the Text Prompt linked to by @trashgod above can be found in this [Text Prompt](http://tips4java.wordpress.com/2009/11/29/text-prompt/) blog entry. – camickr Feb 17 '13 at 01:30

3 Answers3

13

A possible technique is to first set the default string as the text of the textField:

JTextField myField = new JTextField("Default Text");

Then use a FocusListener, so that when the user put the focus in the element, the text disappears:

myField.addFocusListener(new FocusListener() {
    public void focusGained(FocusEvent e) {
        myField.setText("");
    }

    public void focusLost(FocusEvent e) {
        // nothing
    }
});

But you have to be careful: if the user never put the focus in the text field, getText() will return the default string. Therefore, you'd better manage a boolean that tells if the text field has ever had the focus.

Cyrille Ka
  • 15,328
  • 5
  • 38
  • 58
  • This was really helpful. I manage to solve my problem. Thank you so much! – Gustavo Baiocchi Costa Feb 17 '13 at 00:21
  • This is basically what I ended up doing, but in the `focusLost` function I checked if the text field was empty, and if it was, I set the text to the default value and reset the `wasFocused` boolean. – Astrolamb Dec 24 '18 at 17:43
5

I believe what you want is input hint in the text field, something like the image below:

input hint

Check xswingx library which can do this.

iTech
  • 18,192
  • 4
  • 57
  • 80
  • thats exactly what I want :D thx, I will try to find it – Gustavo Baiocchi Costa Feb 16 '13 at 23:42
  • Found this a bit hard to use. THis prompt class is not part of the API of java? plus i want to use with jtextfield – Gustavo Baiocchi Costa Feb 16 '13 at 23:48
  • 2
    This is now apart of the SwingX library. @GustavoBaiocchiCosta No kits not part of the standard library. Better to downloads the latest version of SwingX (IMHO), and, from memory, its as simply as call PromptSupport.setPromot("Prompt", textfield). Check link from my comment in the man section for an example – MadProgrammer Feb 17 '13 at 02:38
  • I have managed to implement this on my own. Thank everyone for the help. I used focus listener to know when someone is pressing on the text field :D – Gustavo Baiocchi Costa Jul 09 '13 at 13:29
2

Would textFieldInstance.setToolTip help:

textFieldInstance.setToolTip("Tool tip for text field");
hd1
  • 33,938
  • 5
  • 80
  • 91