4

我对 JavaFX 很陌生,只是尝试了属性绑定。它适用于 StringProperty 和 BooleanProperty,但现在我必须将一些数据绑定到 ComboBox。

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

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

这是根类:

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)

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

4

1 回答 1

4

从昨天开始,我就明白了。唯一的问题是,我忘记在将 ObservableList 绑定到 ComboBox 之前对其进行初始化。所以,这是缺少的一行, fmo 最开始:

root.setItems(FXCollections.observableArrayList());
于 2013-10-17T15:24:09.163 回答