【发布时间】:2016-08-31 09:01:52
【问题描述】:
我正在尝试使用 ListView 作为字符串的编辑器,它来自自定义数据模型。我将TextFieldListCells 与适当的StringConverter 一起用于单元格。
ListView 旁边有一个添加按钮,它在操作时调用此方法:
@FXML
private void addElement() {
WordListItem newItem = new WordListItem(-1, "");
wordListItems.add(newItem);
wordListView.setEditable(true);
wordListView.getSelectionModel().select(wordListItems.indexOf(newItem));
wordListView.edit(wordListItems.indexOf(newItem));
wordListView.setEditable(false);
}
其中wordListView 是ListView,wordListItems 是包含wordListView 数据的ObservableList。
这确实有效,除了列表为空(非空)时,我无法完全解释原因,因此我检查了 Java 源代码以寻求帮助。
到目前为止,我发现:edit(int) 调用 ListView 更改了 ListViews 内部 editIndex 值,它应该调用 EDIT_START Event。 editIndex 是 ReadOnlyIntegerWrapper,我在其中发现了一些我不太理解的奇怪代码,我不确定这是否真的产生了错误,或者我只是看不出他们为什么这样做:
@Override
protected void fireValueChangedEvent() {
super.fireValueChangedEvent();
if (readOnlyProperty != null) {
readOnlyProperty.fireValueChangedEvent();
}
}
每当ListView 的editIndex 属性发生更改时,都会调用此方法。问题:readOnlyProperty 为空,因为它没有在任何地方设置。我能找到的唯一设置位置是在 getter 中:
public ReadOnlyIntegerProperty getReadOnlyProperty() {
if (readOnlyProperty == null) {
readOnlyProperty = new ReadOnlyPropertyImpl();
}
return readOnlyProperty;
}
(ReadOnlyIntegerImpl 是一个内部私有类,readOnlyProperty 是它的类型)
现在是我的实际问题:这是一个错误还是我在监督什么?有什么原因我不能像这样在列表中添加和编辑新创建的元素,因为它是空的,还是真的只是这个 getter 还没有被调用?
【问题讨论】: