Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a prompt text property to JTextfield [duplicate]

I am using Netbeans IDE. I want to give the prompt text to JTextfield in such a way that when user enters the text into JTextField it gets cleared and accepts the users input.

like image 298
Jayashri Avatar asked Jun 26 '12 03:06

Jayashri


People also ask

How do I set the prompt text in JavaFX?

To set the prompt text for a TextField use the setPromptText method: txtFld.

Can the program put text in JTextField?

The class JTextField is a component that allows editing of a single line of text.

How do I add text to a TextField in Java?

Use setText to write some text to the text field. Use new JTextField("Some text") to initialize the text field with some text. Use new JTextField(10) to set the default columns of the text field. Use new JTextField("some text", 3) to specify the above properties at once.

What is prompt text in JavaFX?

In this article, we show how to add prompt text to a text field in JavaFX. A prompt text is text that appears in a text field when it is first load but that disappears when a user starts typing into the text field.


2 Answers

I don't know what propt-text-fields David Kroukamp already saw, but with the following code I created those textFields who I know ;)

import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.JTextField;

public class PTextField extends JTextField {

    public PTextField(final String proptText) {
        super(proptText);
        addFocusListener(new FocusListener() {

            @Override
            public void focusLost(FocusEvent e) {
                if(getText().isEmpty()) {
                    setText(proptText);
                }
            }

            @Override
            public void focusGained(FocusEvent e) {
                if(getText().equals(proptText)) {
                    setText("");
                }
            }
        });

    }

}
like image 155
user3033626 Avatar answered Oct 14 '22 01:10

user3033626


You can add a simple focus listener to your textfield, and validate the data of the textfield when focus is Lost something like this:

import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

/**
 *
 * @author David
 */
public class Test extends JFrame {

    private JTextField textField, textField2;

    public Test() {
        createAndShowUI();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                Test test = new Test();
            }
        });
    }

    private void createAndShowUI() {
        setTitle("Test");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        createComponents();
        addComponentsToContentPane();
        addListeners();

        pack();
        setVisible(true);
    }

    private void addComponentsToContentPane() {
        getContentPane().setLayout(new GridLayout(2, 1));

        getContentPane().add(textField);
        getContentPane().add(textField2);
    }

    private void createComponents() {
        textField = new JTextField(10);
        textField2 = new JTextField("Click here to lose focus of above textField");
    }

    private void addListeners() {
        textField.addFocusListener(new FocusListener() {

            @Override
            public void focusGained(FocusEvent fe) {
            }

            @Override
            public void focusLost(FocusEvent fe) {
                if (textField.getText().length() >=1) {
                    JOptionPane.showMessageDialog(null, "You entered valid data");
                    textField.setText("");
                }else {
                    JOptionPane.showMessageDialog(null, "You entered invalid data");
                    textField.grabFocus();//make the textField in foucs again
                }
            }
        });
    }
}

To do this in NetBeans right click on the Component, select Events->Focus->focusLost.

like image 32
David Kroukamp Avatar answered Oct 14 '22 02:10

David Kroukamp