繁体   English   中英

将SetCellValueFactory设置为JavaFX TableColumn中的数据对象

[英]SetCellValueFactory to data object in JavaFX TableColumn

我有一个带有自定义单元格渲染的表格列,此单元格渲染接受一个对象并将其属性渲染为Labels。 问题是我找不到将arraylist中的相同对象传递给列的方法。 这是我的代码:

  //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是代表行的数据的类型。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM