![](/img/trans.png)
[英]JavaFX TableColumn.setCellValueFactory() throws null pointer
[英]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.