Adding a prompt text property to JTextfield
18,312
Solution 1
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.
Solution 2
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("");
}
}
});
}
}
Comments
-
Jayashri almost 2 years
I am using Netbeans IDE. I want to give the prompt text to
JTextfield
in such a way that when user enters the text intoJTextField
it gets cleared and accepts the users input. -
nIcE cOw almost 12 years+1, FocusListener is the way to go.
-
kleopatra almost 12 yearsthis 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
-
David Kroukamp almost 12 years@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
-
kleopatra almost 12 yearsCertainly 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 microsoft.com/en-us/download/details.aspx?id=2695)
-
mKorbel almost 12 years@kleopatra please are you sure
-
nIcE cOw almost 12 years@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 theENTER
key just clear the text of theJTextField
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 :-)