8 votes

How to add a placeholder in a JTextField

I would like to have a text box ( jTextField ) and that when the form is started in JAVA SWING in that text box a informative" text of what is to be filled in that field. And that when clicking or inserting the first character that informative text disappears. I tried with setToolTipText but what it does is when you hover the mouse over the jTextField reports, it does not add it to the Text box.

I tried with nombre.setText("Informativo"); but that way it has the default value this one, and when writing it adds it to the one it had. It does not work for me.

16voto

Paul Vargas Points 1145

What you want is a kind of placeholder . One solution is to use the class TextPrompt by Rob Camick, which is very simple to use. Example:

Code:

public static void main(String[] args) {
    JTextField textField = new JTextField(10);
    TextPrompt placeholder = new TextPrompt("Apellido Paterno", textField);
    placeholder.changeAlpha(0.75f);
    placeholder.changeStyle(Font.ITALIC);
    JOptionPane.showMessageDialog(null, textField);
}

Output:

img1

img2

3voto

Luis Pereira Points 11
  1. Download the plugin: http://plugins.netbeans.org/plugin/70043/placeholder
  2. import com.placeholder.PlaceHolder;
  3. In.. initComponents();

    PlaceHolder holder = new PlaceHolder(tuCampoDeText, "lo que quieras que se muestre");

HolaDevs.com

HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by:

X