JavaFX ComboBox itemsProperty绑定

时间:2013-10-16 12:56:19

标签: java properties javafx

我是JavaFX的新手,只是试过了属性bindigs。它与StringProperty和BooleanProperty一起工作正常,但现在我必须将一些数据绑定到ComboBox。

这就是它死去的路线。它位于FXML的控制器类中:

comboBox.itemsProperty().bind(root.itemsProperty());

这是root的类:

import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.collections.ObservableList;

public class SectionComboBox extends SectionControl{

    private final ObjectProperty<ObservableList> items = new SimpleObjectProperty<>();
    }

    public ObservableList getItems() {
        return items.get();
    }

    public void setItems(ObservableList value) {
        items.set(value);
    }

    public ObjectProperty itemsProperty() {
        return items;
    }
}

错误日志:

java.lang.NullPointerException
at com.sun.javafx.scene.control.skin.ComboBoxListViewSkin.getIndexOfComboBoxValueInItemsList(ComboBoxListViewSkin.java:430)
at com.sun.javafx.scene.control.skin.ComboBoxListViewSkin.updateDisplayNode(ComboBoxListViewSkin.java:376)
at com.sun.javafx.scene.control.skin.ComboBoxListViewSkin.getDisplayNode(ComboBoxListViewSkin.java:232)
at com.sun.javafx.scene.control.skin.ComboBoxBaseSkin.updateDisplayArea(ComboBoxBaseSkin.java:125)
at com.sun.javafx.scene.control.skin.ComboBoxBaseSkin.computePrefHeight(ComboBoxBaseSkin.java:180)
at javafx.scene.Parent.prefHeight(Parent.java:879)
at javafx.scene.layout.Region.prefHeight(Region.java:1384)
at javafx.scene.control.Control.computePrefHeight(Control.java:856)
at javafx.scene.Parent.prefHeight(Parent.java:879)
at javafx.scene.control.Control.prefHeight(Control.java:738)
at javafx.scene.layout.Region.computeChildPrefAreaHeight(Region.java:1599)
at javafx.scene.layout.AnchorPane.computeChildHeight(AnchorPane.java:296)
at javafx.scene.layout.AnchorPane.layoutChildren(AnchorPane.java:327)
at javafx.scene.Parent.layout(Parent.java:1018)
at javafx.scene.Parent.layout(Parent.java:1028)
at javafx.scene.Parent.layout(Parent.java:1028)
at javafx.scene.Parent.layout(Parent.java:1028)
at javafx.scene.Parent.layout(Parent.java:1028)
at javafx.scene.Scene.layoutDirtyRoots(Scene.java:516)
at javafx.scene.Scene.doLayoutPass(Scene.java:487)
at javafx.scene.Scene.access$3900(Scene.java:170)
at javafx.scene.Scene$ScenePulseListener.pulse(Scene.java:2186)
at com.sun.javafx.tk.Toolkit.firePulse(Toolkit.java:363)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:461)
at com.sun.javafx.tk.quantum.QuantumToolkit$9.run(QuantumToolkit.java:330)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:17)
at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:67)
at java.lang.Thread.run(Thread.java:724)

有人可以帮助我如何正确地绑定它,我错过了什么?

1 个答案:

答案 0 :(得分:3)

从昨天开始,我已经弄明白了。唯一的问题是,我忘了在将ObservableList绑定到ComboBox之前初始化它。所以,这是缺失的一条线,一开始就是fmo:

root.setItems(FXCollections.observableArrayList());