-1

Is is possible to have some text fields in Java Swing that have a text that show what you have to enter and after typing the first character, it disappears?

like this, that "enter the text" disappears:

https://i.stack.imgur.com/d5kFf.png

I just found setText and setToolTipText for JTextField that are not what I want.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • 1
    I think you'd need to implement the Document that the text field uses. It should show a string when it's empty and should remove that string when the user adds a character, and re-display the string when the users empties the document. You also might be able to do something with the text field itself, but at that level the text field and the document are separate and you'd still need to track the document to know when to display the string. – markspace May 30 '23 at 17:30
  • Note the example in the documentation (it's the first example code) that adds a document listener to a text field. Probably something like that is what you want. https://docs.oracle.com/en/java/javase/17/docs/api/java.desktop/javax/swing/JTextField.html – markspace May 30 '23 at 17:32
  • And here's how to add a background image to a text field, in case that's easier: https://stackoverflow.com/questions/21126994/how-to-add-background-image-to-jtextfield – markspace May 30 '23 at 17:34
  • Check out [Text Prompt](https://tips4java.wordpress.com/2009/11/29/text-prompt/) for one approach. – camickr May 30 '23 at 19:08

1 Answers1

0

While you could add a component on top of a JTextField, I think it’s easier just to draw the placeholder text directly:

public class PlaceholderTextField
extends JTextField {
    private static final long serialVersionUID = 1;

    private String placeholderText = "";

    public PlaceholderTextField() {
        // Deliberately empty.
    }

    public PlaceholderTextField(int columns) {
        super(columns);
    }

    public PlaceholderTextField(String text) {
        super(text);
    }

    public PlaceholderTextField(String text,
                                int columns) {
        super(text, columns);
    }

    public PlaceholderTextField(Document doc,
                                String text,
                                int columns) {
        super(doc, text, columns);
    }

    public String getPlaceholderText() {
        return placeholderText;
    }

    public void setPlaceholderText(String text) {
        this.placeholderText =
            Objects.requireNonNull(text, "Text cannot be null.");
    }

    @Override
    protected void paintComponent(Graphics graphics) {
        super.paintComponent(graphics);

        if (getDocument().getLength() > 0 || placeholderText.isEmpty()) {
            return;
        }

        Rectangle2D textStart;
        try {
            textStart = modelToView2D(0);
        } catch (BadLocationException e) {
            throw new RuntimeException(e);
        }

        Graphics2D g = (Graphics2D) graphics;

        Shape oldClip = g.getClip();
        Composite oldComposite = g.getComposite();

        Insets insets = getInsets();
        g.clipRect(insets.left, insets.top,
            getWidth() - insets.right - insets.left,
            getHeight() - insets.bottom - insets.top);
        g.setComposite(
            AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));

        float x = (float) textStart.getMinX();
        float y = (float) textStart.getMaxY() - g.getFontMetrics().getDescent();
        g.drawString(placeholderText, x, y);

        g.setComposite(oldComposite);
        g.setClip(oldClip);
    }
}
VGR
  • 40,506
  • 4
  • 48
  • 63