Binding 无法设置Javafx combobox绑定值异常

Binding 无法设置Javafx combobox绑定值异常,binding,combobox,javafx,runtimeexception,Binding,Combobox,Javafx,Runtimeexception,我正在尝试使用带有单元格工厂的javafx组合框来呈现列表,我在我的单元格列表的override updateItem()上使用了setText,但是我发现当我在基础模型上更改一个值时,这并不正确ẗ A影响组合框上的已部署列表。因此,我尝试使其绑定属性和它的工作原理,但当尝试清除选择时,我有一个例外。代码如下: import javafx.application.Application; import javafx.beans.property.SimpleStringProperty;

我正在尝试使用带有单元格工厂的javafx组合框来呈现列表,我在我的单元格列表的override updateItem()上使用了setText,但是我发现当我在基础模型上更改一个值时,这并不正确ẗ A影响组合框上的已部署列表。因此,我尝试使其绑定属性和它的工作原理,但当尝试清除选择时,我有一个例外。代码如下:

    import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.util.Callback;

public class BasicComboBoxSample extends Application {
    public static void main(String[] args) { launch(args); }

    @Override public void start(Stage stage) {
        final Employee john = new Employee("John");
        final Employee jill = new Employee("Jill");
        final Employee jack = new Employee("Jack");

        final ComboBox<Employee> cboEmployees = new ComboBox();

        cboEmployees.getItems().addAll(john, jill, jack);
        cboEmployees.setValue(jill);

        Button b = new Button("ChangeName");
        b.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                john.setName("Maria");
            }
        });

        Button c = new Button("Clear");
        c.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                cboEmployees.getSelectionModel().clearSelection();
            }
        });

        Button d = new Button("Select First");
        d.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                cboEmployees.getSelectionModel().select(john);
            }
        });

        Callback<ListView<Employee>, ListCell<Employee>> cellFactory = new Callback<ListView<Employee>, ListCell<Employee>>() {
            @Override
            public ListCell<Employee> call(ListView<Employee> listView) {
                return new EmployeeListCell();  //To change body of implemented methods use File | Settings | File Templates.
            }
        };

        cboEmployees.setButtonCell(new EmployeeListCell());
        cboEmployees.setCellFactory(cellFactory);

        final StackPane layout = new StackPane();
        VBox v = new VBox();

        v.getChildren().add(cboEmployees);
        v.getChildren().add(b);
        v.getChildren().add(c);
        v.getChildren().add(d);
        layout.getChildren().add(v);

        layout.setStyle("-fx-background-color: cornsilk; -fx-padding: 15;");
        stage.setScene(new Scene(layout));
        stage.show();
    }

    class Employee {
        public Employee(String name) { this.setName(name); }
        private SimpleStringProperty name = new SimpleStringProperty("");

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

        SimpleStringProperty nameProperty() {
            return name;
        }

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


    public class EmployeeListCell extends ListCell<Employee> {

        @Override
        protected void updateItem(Employee emp, boolean b) {
            super.updateItem(emp, b);
            if(emp != null){
                textProperty().bind(emp.nameProperty());
            }
        }
    }
}

如果emp为null,则需要解除对textProperty的绑定

在EmployeeListCell类中添加条件

else {
textProperty().unbind();
}
else {
textProperty().unbind();
}