-1

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 JTextFieldand the String but don't know how to do it with the JDialog.

  1. How do we resolve this error and what went wrong here?
4

1 回答 1

0

好吧,我设法通过将以下内容传递给NotEmptyValidator类中的构造函数来解决问题。下面是我得到它的方法:

        this.field.setInputVerifier(new NotEmptyValidator(new JDialog(this), this.field,
            "No Empty Value"));
于 2015-03-18T06:11:07.617 回答