Java: Add Place Holder on JTextField

2.6k views Asked by At

Is there a way or method in which we can add placeholder in j text field. I want to add placeholder "Enter Your Number" in field but how can I do this. I check all methods but didn't working.
Code:

public class Loop extends JFrame{

    private JTextField t1;

        public L(){

        getContentPane().setLayout(null);
        t1=new JTextField();
        t1.setBounds(27,50,47,28);
        getContentPane().add(t1);

        setSize(400,400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);

    }

      }

Main:

public class Main {

    public static void main(String[] args) {

        L object = new L();
    }

    }
3

There are 3 answers

2
camickr On BEST ANSWER

Check out Text Prompt for a flexible solution.

You can control when prompt is displayed (always, focus gained or focus lost). You can also customize the style of the text.

5
maskacovnik On

This code should work, it listen on first click and removes the text

public class Loop extends JFrame{
    private JTextField t1;
    private boolean clicked = false;
    public L(){
        getContentPane().setLayout(null);
        t1=new JTextField();
        t1.setText("Enter Your Number");
        t1.addMouseListener(new MouseAdapter(){
            @Override
            public void mousePressed(MouseEvent e){
                if(!clicked){
                    clicked=true;
                    t1.setText("");
                }
            }
        }
        t1.setBounds(27,50,47,28);
        getContentPane().add(t1);
        setSize(400,400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);
    }
}

Maybe better solution exists
Note - not tested


EDIT (how the boolean clicked works)
when you call method mousePressed(MouseEvent) at the first time, the clicked variable is false, by declaration:

private boolean clicked = false;

So the if body is executed (because !clicked = !false = true)
in the if body, the clicked variable is set to true, so if condition will be then false: (because !clicked = !true = false)
This solves the problem of running code just once.

0
Abder KRIMA On

Here is an example of which you can you inspire

package TinyOS;
import java.awt.*;
import javax.swing.*;
import javax.swing.text.Document;

@SuppressWarnings("serial")
public class PlaceholderTextField extends JTextField {

public static void main(final String[] args) {
    final PlaceholderTextField tf = new PlaceholderTextField ("");
    tf.setColumns(20);
    tf.setPlaceholder("Here is a placeHolder!");
    final Font f = tf.getFont();
    tf.setFont(new Font(f.getName(), f.getStyle(), 30));
    JOptionPane.showMessageDialog(null, tf);
}

private String placeholder;

public PlaceholderTextField () {
}

public PlaceholderTextField (
    final Document pDoc,
    final String pText,
    final int pColumns)
{
    super(pDoc, pText, pColumns);
}

public PlaceholderTextField (final int pColumns) {
    super(pColumns);
}
}

I hope that can help you