0

I'm really surprised this is not in the documentation or at least google.

I have a class that is likely to need to remove the verifier or replace it with another one. In particular, these methods are defined in the interface:

  /**
   * Add the verifier
   */
  public void bind();
  /**
   * Remove the verifier from input
   */
  public void unbind();

I can implement bind:

  /**
   * Binds the events to the field using InputVerifier
   */
  @Override
  public void bind() {
    //Internal verifier
    final SettingsInputVerifier<T> verif = this.verifier;
    //Event to be called if new value is valid
    final ValueChanged<T> onchange = this.onchange;
    //Only works when you leave the field
    field.setInputVerifier(new InputVerifier() {
      @Override
      public boolean verify(JComponent in) {
        //If verification fails, return false and ignore the value
        if(!verif.verify(in))
          return false;
        //Sucessful verification means we get the value and update it
        onchange.changed(verif.value(in));
        return true;
      }
    });
  }

But how can I unset input verifier from JTextField?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778

1 Answers1

2

Try this way:

field.setInputVerifier(null);
Paco Abato
  • 3,920
  • 4
  • 31
  • 54
  • In other words, if I want to replace old verifier with the new, I just call `.setInputVerifier(newverifier)`? – Tomáš Zato Feb 27 '15 at 09:13
  • That's right. The new verifier replaces the old one (you can look at the code of [JComponent](http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/javax/swing/JComponent.java#JComponent.setInputVerifier%28javax.swing.InputVerifier%29)) and if it is null it is removed. – Paco Abato Feb 27 '15 at 09:18