5

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();
    }

    }
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

3 Answers3

2

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

Abder KRIMA
  • 3,418
  • 5
  • 31
  • 54
2

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.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • could you please make code of TextPrompt place holder in my above code i'm stuck at this. –  Jun 15 '15 at 17:32
  • @TimAllen, its one line of code to use the `TextPrompt` with its default settings. I don't know how you are stuck. Update your showing what you tried and explain what is not working the way you want. – camickr Jun 15 '15 at 18:59
1

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.

maskacovnik
  • 3,080
  • 5
  • 20
  • 26
  • can u please tell me in simple words how boolean works on if condition –  Jun 15 '15 at 15:13
  • if(true){} or boolean p=true; if(p){} or if (!p){} – Abder KRIMA Jun 15 '15 at 15:14
  • 1
    Don't use a null layout. Not a complete solution. What happens if the user tabs to the text field? A GUI should be designed to work whether the user uses a mouse or the keyboard. What happens if the user clicks on the field and then clicks on another field? The text is removed but never replaced. – camickr Jun 15 '15 at 15:16
  • @TinyOS if boolean p = false if(!p) then true –  Jun 15 '15 at 15:17
  • no !p gives false : here is an example `public static void main(String[] args) { boolean testva=false; if(testva){ System.out.println("gives true"); } if(!testva){ System.out.println("gives false"); } }` – Abder KRIMA Jun 15 '15 at 15:24