JavaFX ChoiceBox 不显示初始值

JavaFX ChoiceBox doesn't show initial value

我有一个工厂方法,它为我提供了一个双向绑定到枚举的 ChoiceBox 属性。选择框按预期工作,只是它不显示 属性 构造函数中设置的初始值。相反,选择框最初显示为空白,不显示任何值。怎么了?

这是一个 MRE:

import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.scene.Scene;
import javafx.scene.control.ChoiceBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class ChoiceBoxMRE extends Application {

    private ObjectProperty<Table> table = new SimpleObjectProperty<>(Table.BIG);

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

    @Override
    public void start(Stage primaryStage) {

        VBox vBox = new VBox();
        vBox.getChildren().add(getChoiceBox(table));
        primaryStage.setScene(new Scene(vBox));
        primaryStage.show();
    }

    private <T extends Enum<T>> ChoiceBox<T> getChoiceBox(ObjectProperty<T> objectProperty) {
        ChoiceBox<T> choiceBox = new ChoiceBox<>();
        choiceBox.getItems()
                .addAll(objectProperty.getValue().getDeclaringClass().getEnumConstants());
        Bindings.bindBidirectional(objectProperty, choiceBox.valueProperty());
        return choiceBox;
    }

    enum Table {
        BIG, SMALL;
    }
}

我不确定这是否是错误,但有一个简单的解决方法:只需 select 绑定之前的值:

choiceBox.getSelectionModel().select(objectProperty.get());