TextFormatter 在绑定(双向)到空字符串时抛出异常

TextFormatter throws exception when binding (bidirectional) to a null string

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


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

java.lang.IllegalArgumentException: The start must be <= the end

在调用 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 的文本 属性 - 而不是在旧对象之前Model 未绑定。该模型交替使用 "foo" 作为名称进行初始化,或者不使用名称进行初始化 - 即 - 名称保持为空。

第一次单击该按钮时,您应该会看到文本更改为 "foo",而当下次单击该按钮时,将抛出异常。

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

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);
    }

}