【问题标题】:TextFormatter throws exception when binding (bidirectional) to a null string绑定(双向)到空字符串时,TextFormatter 引发异常
【发布时间】:2016-01-21 13:16:38
【问题描述】:

编辑
显然这是一个错误。我所做的报告可以找到here。正如@James_D 所指出的,这不是绑定的问题,但是在将文本设置为非空值后将其设置为空就足够了。


我在使用 JavaFX TextFormatter 时遇到问题。我想将文本字段中的文本长度限制为 10 个字符,但我发现如果文本属性绑定到非空值,然后未绑定并重新绑定到空值,则文本格式化程序会引发异常:

java.lang.IllegalArgumentException:开始必须是

在调用TextFormatter.Change#getControlNewText 时,这很奇怪,因为如果有的话,我会期待一个空引用异常。

我附上了一个简单的代码,用于展示这个问题的完整示例。如果我做错了什么,请告诉我

package sample;

import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;

import javafx.stage.Stage;

public class Main extends Application {

    private Model m;
    private int num = 0;

    @Override
    public void start(Stage primaryStage) throws Exception {

        TextField tf = new TextField();
        tf.setTextFormatter(new TextFormatter<>(change -> change.getControlNewText().length() > 10 ? null : change));
        Button b = new Button("Click!");
        b.setOnAction(ev -> {
                    if (m != null) {
                        tf.textProperty().unbindBidirectional(m.nameProperty());
                    }

                    m = new Model();
                    if (num % 2 == 0) {
                        System.out.println("Setting foo");
                        m.setName("foo");
                    }
                    num++;

                    tf.textProperty().bindBidirectional(m.nameProperty());
                }
        );
        VBox vb = new VBox(tf, b);

        primaryStage.setScene(new Scene(vb));
        primaryStage.show();


    }

    public class Model {
        private SimpleStringProperty name = new SimpleStringProperty(this, "name");

        public StringProperty nameProperty() {
            return name;
        }

        public String getName() {
            return name.get();
        }

        public void setName(String name) {
            this.name.set(name);
        }

    }


    public static void main(String[] args) {
        launch(args);
    }

}

在此代码中,有一个 TextField 和一个 TextFormatter 拒绝所有更改,导致长度>10 的字符串。单击按钮时,会创建一个新的 Model 对象,并且它的 name 属性绑定到 TextField 的 text 属性 - 而不是在旧的 Model 未绑定之前。该模型交替使用“foo”作为名称进行初始化,或者不使用名称进行初始化 - 也就是说 - 名称保持为空。

第一次单击按钮时,您应该会看到文本更改为“foo”,而当下次单击按钮时,会引发异常。

【问题讨论】:

  • 嗯,这是一个错误。但即使它有效,这段代码似乎也有点冒险。您正在设置双向绑定,这表明两个字符串属性应始终保持相同的值,但是文本格式化程序否决了对其中一个属性的一些更改。因此,如果这“成功”,可能会发生潜在的坏事:您希望 m.setName("More than 10 characters"); 有什么行为?
  • 好吧,我不希望它发生 :) 但你说得对,这是一个问题。不过,它与此错误无关。你知道这是否已经被报道了吗?或者,如果我有办法在此期间绕过它?
  • 我不知道,但我已经有一段时间没有积极关注错误报告了。如果您没有找到任何东西,可以很容易地进行快速搜索并提交报告。我想我的意思是我试图弄清楚是否存在将(否决)格式化程序附加到双向绑定文本字段的有效用例;但你是对的,即使它根本没有否决权,它也会失败:只要你打电话给getControlNewText
  • 找不到类似的错误。我已经提交了一份报告,但可能需要几天时间才能提交,就像“webbug”报告一样。如果它被归档,我会在这里发布。
  • 你能避免模型属性为空作为一种解决方法(例如使用零长度字符串来初始化它)吗?

标签: java javafx binding


【解决方案1】:

这看起来像一个错误(似乎文本格式化程序的过滤器无法正确处理设置为 null 的文本)。一种可能的解决方法是绑定文本格式化程序的 value 属性,而不是文本字段的 text 属性:

import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;

import javafx.stage.Stage;

public class Main extends Application {

    private Model m;
    private int num = 0;

    @Override
    public void start(Stage primaryStage) throws Exception {

        TextField tf = new TextField();
        TextFormatter<String> textFormatter = new TextFormatter<>(
                TextFormatter.IDENTITY_STRING_CONVERTER, "", change -> 
            change.getControlNewText().length() > 10 ? null : change);

        tf.setTextFormatter(textFormatter);

        Button b = new Button("Click!");
        b.setOnAction(ev -> {
                    if (m != null) {
//                        tf.textProperty().unbindBidirectional(m.nameProperty());
                       textFormatter.valueProperty().unbindBidirectional(m.nameProperty());
                    }

                    m = new Model();
                    if (num % 2 == 0) {
                        System.out.println("Setting foo");
                        m.setName("foo");
                    } 
                    num++;

//                    tf.textProperty().bindBidirectional(m.nameProperty());
                    textFormatter.valueProperty().bindBidirectional(m.nameProperty());
                }
        );
        VBox vb = new VBox(tf, b);

        primaryStage.setScene(new Scene(vb));
        primaryStage.show();


    }

    public class Model {
        private SimpleStringProperty name = new SimpleStringProperty(this, "name", "");

        public StringProperty nameProperty() {
            return name;
        }

        public String getName() {
            return name.get();
        }

        public void setName(String name) {
            this.name.set(name);
        }

    }


    public static void main(String[] args) {
        launch(args);
    }

}

【讨论】:

  • 我刚刚想通了,正要发布同样的内容。它似乎工作得很好。
猜你喜欢
  • 1970-01-01
  • 2023-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-21
  • 2012-03-31
  • 2015-11-07
相关资源
最近更新 更多