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.
To set the prompt text for a TextField use the setPromptText method: txtFld.
The class JTextField is a component that allows editing of a single line of text.
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.
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.
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("");
}
}
});
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With