5

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.

kleopatra
  • 51,061
  • 28
  • 99
  • 211
Jayashri
  • 366
  • 4
  • 13
  • 25

2 Answers2

4

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

    }

}
2

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.

David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
  • +1, FocusListener is the way to go. – nIcE cOw Jun 26 '12 at 04:54
  • 3
    this is not an answer to the question - at least not as I understand it. The OP is not asking about validation, but about a "prompt" which is shown as long as the field has neither input nor focus, typically in a gray color. @nIcEcOw - yeah, focusListener is involved in prompt support but not the whole story. SwingX comes with prompt support :-) On the other hand, if you really want validation, a bare-bones focusListener is too low-level, at least use a InputVerifier – kleopatra Jun 26 '12 at 08:36
  • @Kleopatra I do not think it deserved a down vote, because you read the questions differently then me, the OP said 'in such a way that when user enters the text into JTextField it gets cleared and accepts the users input' he does not talk about a greyed out anything! and he asked how to add it using netbeans, which Im guessing his using a IDE to build the UI and I showed the exact procedures the OP asked to have using the focusListener – David Kroukamp Jun 26 '12 at 09:29
  • Certainly will revert the downvote if mis-interpreted the OP :-) though, "prompt" is a technical term, not much leeway for interpretation, IMO. (see f.i. the chapter on text boxes in http://www.microsoft.com/en-us/download/details.aspx?id=2695) – kleopatra Jun 26 '12 at 09:40
  • 2
    @kleopatra [please are you sure](http://tips4java.wordpress.com/2009/11/29/text-prompt/) – mKorbel Jun 26 '12 at 11:15
  • 1
    @kleopatra : Welcome back after a long gap. The OP never specified, if there is any way to validate the input, if so than what sort of an input is Valid. So if I understood the way the OP wants it to be, then I can simply go for an `ActionListener`, and on the press of the `ENTER` key just clear the text of the `JTextField` and save the value in a variable.. But on the first glance, `FocusListener` is what seems fit to do that job for me. So I still stand with my +1 as before :-) – nIcE cOw Jun 26 '12 at 14:09