如何引用CellFactory上的细胞数据?
How to reference cell data on CellFactory?
我有一个 TableView
,其中最后一列是“操作”列,其中包含一个自定义 ActionBox
对象,带有多个操作按钮。要向此 ActionBox
添加行为,我需要向其传递一个 Data
对象。但是,我不知道如何引用该对象。
class TableViewWithActionColumn() {
@FXML TableColumn<Data, Void> actionColumn;
public TableViewWithActionColumn() {
// Code for loading custom component...
}
@FXML
public void initialize() {
populateActionColumn();
}
private void populateActionColumn() {
Callback<TableColumn<Data, Void>, TableCell<Data, Void>> cellFactory = new Callback<TableColumn<Data, Void>, TableCell<Data, Void>>() {
@Override
public TableCell<Data, Void> call(final TableColumn<Data, Void> param) {
return new TableCell<Data, Void>() {
private final ActionBox actionBox = new ActionBox();
@Override
public void updateItem(Void item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setGraphic(null);
} else {
setGraphic(actionBox);
}
}
};
}
};
actionColumn.setCellFactory(cellFactory);
}
}
我假设对对象的引用在 Void item
中,所以我尝试用 Data
替换所有出现的 Void
,并执行 setGraphic(new ActionBox(item));
,但是这导致 NullPointerException,所以我认为这不是正确的方法。那么,如何在 CellFactory 上下文中引用行的数据?
TableCell
有一个 getTableRow()
方法,它为您提供对包含单元格的 TableRow
的引用。 TableRow
本身就是一个单元格实现,所以你可以调用它的 getItem()
方法来获取行所代表的数据。
在上下文中(并从您的代码中删除所有不必要的样板文件):
private void populateActionColumn() {
actionColumn.setCellFactory(col -> new TableCell<Data, Void>() {
private final ActionBox actionBox = new ActionBox();
@Override
public void updateItem(Void item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setGraphic(null);
} else {
Data rowData = getTableRow().getItem();
// ... something like: actionBox.setData(rowData) ;
setGraphic(actionBox);
}
}
});
}
我有一个 TableView
,其中最后一列是“操作”列,其中包含一个自定义 ActionBox
对象,带有多个操作按钮。要向此 ActionBox
添加行为,我需要向其传递一个 Data
对象。但是,我不知道如何引用该对象。
class TableViewWithActionColumn() {
@FXML TableColumn<Data, Void> actionColumn;
public TableViewWithActionColumn() {
// Code for loading custom component...
}
@FXML
public void initialize() {
populateActionColumn();
}
private void populateActionColumn() {
Callback<TableColumn<Data, Void>, TableCell<Data, Void>> cellFactory = new Callback<TableColumn<Data, Void>, TableCell<Data, Void>>() {
@Override
public TableCell<Data, Void> call(final TableColumn<Data, Void> param) {
return new TableCell<Data, Void>() {
private final ActionBox actionBox = new ActionBox();
@Override
public void updateItem(Void item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setGraphic(null);
} else {
setGraphic(actionBox);
}
}
};
}
};
actionColumn.setCellFactory(cellFactory);
}
}
我假设对对象的引用在 Void item
中,所以我尝试用 Data
替换所有出现的 Void
,并执行 setGraphic(new ActionBox(item));
,但是这导致 NullPointerException,所以我认为这不是正确的方法。那么,如何在 CellFactory 上下文中引用行的数据?
TableCell
有一个 getTableRow()
方法,它为您提供对包含单元格的 TableRow
的引用。 TableRow
本身就是一个单元格实现,所以你可以调用它的 getItem()
方法来获取行所代表的数据。
在上下文中(并从您的代码中删除所有不必要的样板文件):
private void populateActionColumn() {
actionColumn.setCellFactory(col -> new TableCell<Data, Void>() {
private final ActionBox actionBox = new ActionBox();
@Override
public void updateItem(Void item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setGraphic(null);
} else {
Data rowData = getTableRow().getItem();
// ... something like: actionBox.setData(rowData) ;
setGraphic(actionBox);
}
}
});
}