javafx treetableview单元格值未更新 [英] javafx treetableview cell value not getting updated
问题描述
更新数据模型javafx treetableview单元格值后事件未更新.
Event after updating the data model javafx treetableview cell value not getting updated.
I am using the sample code here @ http://docs.oracle.com/javase/8/javafx/user-interface-tutorial/tree-table-view.htm (Example 15-2)
在单击按钮时,我试图更新第一项:employee.get(0).setName("Test");
On click of button i m trying to update first item: employees.get(0).setName("Test");
是否有任何技巧可以使用更新的treetableview?
Is there any trick using which treetableview can be updated?
推荐答案
该示例有些奇怪,返回了一个ReadOnlyStringWrapper
,其中包装了单元格值工厂的属性值.因此,它没有将列中显示的值直接绑定到Employee
类中的属性,而是将它们绑定到一个新的只读属性,该属性包装了在单元格上调用updateItem(..)
时检索到的值.这意味着在更新基础数据时不会更新,而仅在单元格上调用updateItem(...)
方法时才更新. (我不知道他们为什么会这样做.)因此,例如,您应该发现,如果更改值,然后将TreeTableView
中的根节点折叠,然后再次展开,则在展开后将显示新值根(因为这会导致调用单元格的updateItem(...)
方法).
The example, somewhat strangely, returns a ReadOnlyStringWrapper
wrapping the property values for the cell value factories. Thus instead of binding the value displayed in the column directly to the properties in the Employee
class, it binds them to a new, read-only, property wrapping the value retrieved when updateItem(..)
is called on the cell. This means it won't get updated when the underlying data is updated, but only if the updateItem(...)
method is invoked on the cell. (I have no idea why they would do this.) So you should find, for example, that if you change the value, then collapse the root node in the TreeTableView
and expand it again, that your new value is displayed after expanding the root (because this causes the cells' updateItem(...)
methods to be invoked).
要使单元格在数据更改时更新,请将单元格值直接绑定到模型(Employee
)类中定义的属性:
To make the cells update when the data is changed, bind the cell value directly to the property defined in the model (Employee
) class:
empColumn.setCellValueFactory( param -> param.getValue().getValue().nameProperty());
和
emailColumn.setCellValueFactory( param -> param.getValue().getValue().emailProperty());
这篇关于javafx treetableview单元格值未更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!