Adding a query text property to a JTextfield

I am using the Netbeans IDE. I want to give the JTextfield hint JTextfield in such a way that when the user enters text in the JTextfield , it is cleared and accepts the user input.

+4
source share
2 answers

You can add a simple focus listener to your text field and check the text field data when the focus is lost. Here is 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 Component , select Events-> Focus-> focusLost.

+2
source

I don’t know what kind of protective text fields David Crowcamp has already seen, but with the following code I created those text fields that 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(""); } } }); } } 
+3
source

All Articles