gpt4 book ai didi

JavaFx - 为什么不能重写方法 "updateItem"?

转载 作者:行者123 更新时间:2023-12-01 16:59:04 25 4
gpt4 key购买 nike

我尝试将图像添加到 TabelView 中的单元格。我读了一些解决方案(例如: Placing an image into a JavaFX 2 table ),它们都必须覆盖方法“updateItem”,但它对我来说不存在。我的错误是什么?

这里是代码:

public class DynamicTable extends TableView<Student>{

private ObservableList<Student> data;
private int columnCount;
private String[] columnName;
private Student[] rows;
//private TableView tableView;

DynamicTable(){

this.columnName=null;
this.columnCount=0;
this.rows=null;
}

DynamicTable(int columnCount, Student[] rows, String[] columnName){

this.columnName=columnName;
this.columnCount=columnCount;
this.rows=rows;
}

@SuppressWarnings({ "rawtypes", "unchecked" })
public void buildTable(){
setEditable(true);
data = FXCollections.observableArrayList();
for(int i=0 ; i<columnCount; i++){
final int j = i;
if(columnName[i].toLowerCase().contains("pic")){
TableColumn<Student,Image> col = new TableColumn<Student,Image>(columnName[i]);
col.setCellValueFactory(new PropertyValueFactory<Student, Image> (Student.getFieldName(columnName[i])));
col.setCellFactory(new Callback<TableColumn<Student,Image>, TableCell<Student,Image>>() {

@Override
public TableCell<Student, Image> call(TableColumn<Student, Image> param) {
return new TableCell<Student, Image>(){
// Override the method "updateItem". Not work.
};
}
});
this.getColumns().addAll(col);
}
else{
TableColumn<Student,String> col = new TableColumn<Student,String>(columnName[i]);
col.setCellValueFactory(new PropertyValueFactory<Student, String>(i>14?
columnName[i]:Student.getFieldName(columnName[i])));
this.getColumns().addAll(col);
}

}
data.addAll(rows);
setItems(data);
}

最佳答案

您可以使用以下单元工厂 -

 dependency.setCellFactory(new Callback<TableColumn<DevWorkTabBench, String>, TableCell<DevWorkTabBench, String>>() {
@Override
public TableCell<DevWorkTabBench, String> call(TableColumn<DevWorkTabBench, String> col) {
final TableCell<DevWorkTabBench, String> cell = new TableCell<DevWorkTabBench, String>() {
@Override
public void updateItem(String firstName, boolean empty) {
super.updateItem(firstName, empty);
if (empty) {
setText(null);
} else {
// setText(firstName);
setStyle("-fx-text-fill:blue;");
Image imgInstalled = new Image("upgradeworkbench/View/Icons/dependency.png");
setGraphic(new ImageView(imgInstalled));
}
}
};
return cell; }});

关于JavaFx - 为什么不能重写方法 "updateItem"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29164660/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com