假设我有一个名为Employee的类。

Employee具有字符串名称,整数年龄和自定义对象Workstyle道德。工作样式具有专用的字符串样式。

在GUI类中,我们创建TableColumns:

TableColumn<Professional, String> nameColumn = new TableColumn<>("NAME");
nameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));
//
TableColumn<Employee, Integer> ageColumn = new TableColumn<>("AGE");
nameColumn.setCellValueFactory(new PropertyValueFactory<>("age"));
//


现在这是我迷路的地方。我无法做:

TableColumn<Employee, String> workstyleColumn = new TableColumn<>("WORKSTYLE");
workstyleColumn.setCellValueFactory(new PropertyValueFactory<>("ethic.getStyle()"));


必须有以下几种方式:

TableColumn<Object Being Looked At, Type being put onto the column> columnTitle = new TableColumn<>(
{
     // I want to put tons of code here which EVENTUALLY ends with a object of type "Type being put onto the column".
});


谢谢您的帮助!

最佳答案

你需要

workstyleColumn.setCellValueFactory(cellData ->
    new ReadOnlyStringWrapper(cellData.getValue().getEthic().getStyle()));


cellValueFactoryCallback<TableColumn.CellDataFeatures, ObservableValue>,即采用TableColumn.CellDataFeatures并返回ObservableValue的函数。 CellDataFeatures.getValue()提供该行的值,因此cellData.getValue().getEthic().getStyle()提供您想要的值。最后,将其包装在ReadOnlyStringWrapper中以提供ObservableValue

关于java - TableColumn JavaFX的SetPropertyValueFactory,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33364808/

10-09 05:19