【问题标题】:Remove InputVerifier from JTextField从 JTextField 中删除 InputVerifier
【发布时间】:2015-04-29 22:01:41
【问题描述】:

我真的很惊讶this is not in the documentationat least google

我有一个类可能需要删除验证器或将其替换为另一个。特别是,这些方法是在接口中定义的:

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

我可以实现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;
      }
    });
  }

但我如何取消设置 JTextField 的输入验证器?

【问题讨论】:

    标签: java swing jtextfield inputverifier


    【解决方案1】:

    试试这个方法:

    field.setInputVerifier(null);
    

    【讨论】:

    • 也就是说,如果我想用新的验证器替换旧的验证器,我只需调用.setInputVerifier(newverifier)?
    • 没错。新的验证器替换旧的验证器(可以看JComponent的代码),如果为null则删除。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多