JavaFX如何将ComboBox的选定值绑定到集合的成员?

时间:2019-07-12 14:18:25

标签: javafx data-binding combobox properties

假设我具有以下类结构:

ObservableList<Person> persons //all persons

//...

class Person
{
    StringProperty name = new SimpleStringProperty();

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

    public StringProperty nameProperty()
    {
        return name;
    }
}

class Car
{
    StringProperty name = new SimpleStringProperty();
    ListProperty<Person> persons = new SimpleListProperty<>();

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

    public StringProperty nameProperty()
    {
        return name;
    }

    public ObservableList<Person> getPersons()
    {
        persons.get();
    }

    public ListProperty<Person> personsProperty()
    {
        return persons;
    }
}

public Node createUi(Car car)
{
    VBox vbox = new VBox();

    TextField textField = new TextField();
    textField.textProperty.bindBidirectional(car.nameProperty();

    for (Person person : car.getPersons())
    {
        ComboBox<Person> comboBox = new ComboBox();
        comboBox.itemsProperty().bindBidirectional(persons);

        //TODO how to bind to person?
        //comboBox.valueProperty.bindBidirectional( 
    }
}

汽车包含人员列表。

我想提供一个从ComboBox中选择人员的用户界面-每个人都有一个对应的ComboBox。 如何将ComboBox的选定值绑定到当前的Person对象? comboBox.valueProperty.bindBidirectional(期望的类型为Property<Person>,但我使用原始的Person对象进行迭代。

enter image description here

我应该将persons类的Car集合的类型更改为ListProperty<ObjectProperty<Person>>ObservableList<ObservableValue<Person>>

如果相应组合框的选定值已更改,如何更改集合中的人员。

0 个答案:

没有答案