private void initialize() {
loadPersistenceContext();
List<Events> events = getEventsChoiceBox(getPersistenceContext());
ObservableList<Event> data = FXCollections.observableList(events);
cbEvent.setItems(data); // Inserting data into the ChoiceBox
}
这是我的主要代码。问题是加载表单时,我得到的是插入到 ChoiceBox 中的对象,而不是属性。
这是我的列表事件的内容
Object[]
|- String
|- Integer
Object[]
|- String
|- Integer
所以我想要一个 ChoiceBox,其中显示 String 属性并将 Integer 映射到我的控制器。
我尝试了很多东西但无法弄清楚。
这是另一个简单的实现论坛.oracle.com
为键值创建一个类
public class KeyValuePair {
private final String key;
private final String value;
public KeyValuePair(String key, String value) {
this.key = key;
this.value = value;
}
public String getKey() { return key; }
public String toString() { return value; }
}
然后创建 ChoiceBox 为:
ChoiceBox<KeyValuePair> choiceBox = new ChoiceBox<KeyValuePair>();
将元素填充为;
choiceBox .getItems().add(new KeyValuePair("1", "Active"));
提示:从数据库中检索键值对到 ArrayList 中并进行迭代
要检索该值:
choiceBox.getValue().getKey(); // returns the "1"
choiceBox.getValue().toString(); // returns the "Active"
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)