SetCellValueFactory to data object in JavaFX TableColumn



我有一个带有自定义单元格渲染的表列,此单元格渲染采用一个对象并将其属性呈现为标签。问题是我找不到将数组列表中的相同对象传递给列的方法。这是我的代码:

  //I want to render this object in a column as well as use it in the rest of columns
  CustomerCreationFlow cflow=new CustomerCreationFlow();
        cflow.setId(10L);
        cflow.setFirstName("Feras");
        cflow.setLastName("Odeh");
        cflow.setCustomerType("type");
        ObservableList<CustomerCreationFlow> data = FXCollections.observableArrayList(cflow);
        idclm.setCellValueFactory(new PropertyValueFactory<CustomerCreationFlow, String>("id"));
//I tried this but it didn't work
            flowclm.setCellValueFactory(new PropertyValueFactory<CustomerCreationFlow, CustomerCreationFlow>("this"));
            typeclm.setCellValueFactory(new PropertyValueFactory<CustomerCreationFlow, String>("customerType"));
            flowTable.setItems(data);

有什么建议吗?

您应该通过扩展 TableCell 来实现您的自定义 CellFactory。在自定义 TableCell 中,可以通过获取当前 TableCell 的 TableRow 来获取表行的值(逻辑上为 CustomerCreationFlow)。

这给出了:

class MyTableCell<S,T> extends TableCell<S, T>
@Override
public void updateItem(final T item, final boolean empty) {
    super.updateItem(item, empty);
    if (empty) {
        this.setText(null);
        this.setGraphic(null);
    } else {
        S item = (S) this.getTableRow().getItem();
        // DO STUFF HERE
    }
}
}

T 是 CellValueFactory 定义的数据类型。S 是表示行的数据类型。

最新更新