gpt4 book ai didi

JavaFX ComboBox items属性绑定(bind)

转载 作者:搜寻专家 更新时间:2023-11-01 02:47:12 25 4
gpt4 key购买 nike

我是 JavaFX 的新手,刚刚尝试过属性绑定(bind)。它适用于 StringProperty 和 BooleanProperty,但现在我必须将一些数据绑定(bind)到 ComboBox。

这就是线,它死的地方。它位于 FXML 的 Controller 类中:

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)

谁能帮我正确绑定(bind)它,我错过了什么?

最佳答案

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

root.setItems(FXCollections.observableArrayList());

关于JavaFX ComboBox items属性绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19404023/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com