我正在尝试将图像添加到 tableView,并经历了几个问题和答案,但它仍然不起作用。
tableView 中的其他字段(如名称)已正确加载。
Intelij 告诉我 updateItem
从未使用过,这可能就是它不起作用的原因,但我不知道如何调试它......
这是我到目前为止得到的
@FXML private TableColumn<PlayerManager, Image> tableColumnType;
@FXML private void initialize(){
tableColumnType.setCellFactory(param -> {
//Set up the ImageView
final ImageView imageview = new ImageView();
imageview.setFitHeight(10);
imageview.setFitWidth(10);
///imageview.setImage(imageComputer); //uncommenting this places the image on all cells, even empty ones
//Set up the Table
TableCell<PlayerManager, Image> cell = new TableCell<PlayerManager, Image>() {
public void updateItem(PlayerManager item, boolean empty) {
if (item != null) { // choice of image is based on values from item, but it doesn't matter now
imageview.setImage(imageComputer);
}
}
};
// Attach the imageview to the cell
cell.setGraphic(imageview);
return cell;
});
}
我遇到的问题是:
最佳答案
updateItem
的签名方法错误:应该是
public void updateItem(Image item, boolean empty) { /* ... */ }
如果编译器拒绝 @Override
注释,那么您就知道您没有定义正确的方法。所以你应该使用@Override
如果出现编译错误,则表明出现了问题。
所以你应该能够做到
@FXML private TableColumn<PlayerManager, Image> tableColumnType;
@FXML private void initialize(){
tableColumnType.setCellFactory(param -> {
//Set up the ImageView
final ImageView imageview = new ImageView();
imageview.setFitHeight(10);
imageview.setFitWidth(10);
///imageview.setImage(imageComputer); //uncommenting this places the image on all cells, even empty ones
//Set up the Table
TableCell<PlayerManager, Image> cell = new TableCell<PlayerManager, Image>() {
@Override
public void updateItem(Image item, boolean empty) {
if (item != null) { // choice of image is based on values from item, but it doesn't matter now
imageview.setImage(imageComputer);
}
}
};
// Attach the imageview to the cell
cell.setGraphic(imageview);
return cell;
});
}
如果您的表格单元格需要访问实际的PlayerManager
对象,那么您需要将表列设为 TableColumn<PlayerManager, PlayerManager>
并更新cellValueFactory
(你还没有显示)相应地。
最后,请注意您的 updateItem(...)
方法需要处理所有情况,包括项目为空的空单元格。
所以你可能需要类似的东西
@FXML private TableColumn<PlayerManager, PlayerManager> tableColumnType;
@FXML private void initialize(){
tableColumnType.setCellValueFactory(cellData -> new SimpleObjectProperty<PlayerManager>(cellData.getValue());
tableColumnType.setCellFactory(param -> {
//Set up the ImageView
final ImageView imageview = new ImageView();
imageview.setFitHeight(10);
imageview.setFitWidth(10);
///imageview.setImage(imageComputer); //uncommenting this places the image on all cells, even empty ones
//Set up the Table
TableCell<PlayerManager, PlayerManager> cell = new TableCell<PlayerManager, PlayerManager>() {
@Override
public void updateItem(PlayerManager item, boolean empty) {
if (item != null) { // choice of image is based on values from item, but it doesn't matter now
imageview.setImage(imageComputer);
} else {
imageView.setImage(null);
}
}
};
// Attach the imageview to the cell
cell.setGraphic(imageview);
return cell;
});
}
关于java - 将图像放入 tableColumn - updateItem 从未使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39430859/