I am trying to create a JTextField
input validator following Building a Swing Validation Package with InputVerifier. I have taken all the class on that page an am now trying to use the NotEmptyValidator
found here. I have a JFrame
and a JTextField
. I am trying to use this package but its giving me an error.The code is shown below:
public class ValidateTest extends JFrame {
public JTextField field;
public ValidateTest() {
this.field = new JTextField();
this.field.setInputVerifier(new NotEmptyValidator(this, this.field,
"No Empty Value"));// The line showing an error.
this.setTitle("ValidateTest");
this.setVisible(true);
this.setSize(200, 200);
this.setLayout(new BorderLayout());
this.add(this.field, BorderLayout.NORTH);
}
public static void main(String[] args) {
try {
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (Exception e) {
}
ValidateTest vt = new ValidateTest();
}
}
NOTE: I have just taken the classes as they are. The only code I have added is above.
My Questions:
1. How do I instantiate the NotEmptyValidator
? I mean I provided the JTextField
and the String
but don't know how to do it with the JDialog
.
Well I managed to solve the problem by passing the following to the constructor in NotEmptyValidator
class. Below is how I got it:
this.field.setInputVerifier(new NotEmptyValidator(new JDialog(this), this.field,
"No Empty Value"));