javaswingjtextfieldinputverifier

Remove InputVerifier from JTextField


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?


Solution

  • Try this way:

    field.setInputVerifier(null);