JavaFx - 为什么不能覆盖方法“updateItem”?

时间:2015-03-20 10:43:36

标签: java javafx tableview cells

我尝试将Image添加到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);
}

2 个答案:

答案 0 :(得分:3)

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

 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; }});

答案 1 :(得分:2)

为什么不尝试这样简单有效的事情:

public class AddImage extends TableCell<YourEntity, Boolean> {

final StackPane paddedButton = new StackPane();
final HBox hbox = new HBox();
final VBox vbox = new VBox();
Stage primaryStage = new Stage();
Scene scene = new Scene(new Group());


Image img = new Image("../your image path or url ");

public AddImage(final Stage stage, final TableView table) {

  paddedButton.setPadding(new Insets(3));

  vbox.getChildren().add(img);


  hbox.getChildren().add(vbox);

  hbox.setPadding(new Insets(30, 5, 5, 5));

  paddedButton.getChildren().add(hbox);
  //primaryStage.setScene(scene);
  //primaryStage.show();



}
@Override protected void updateItem(Boolean item, boolean empty) {
    super.updateItem(item, empty);
    if (empty) {
setGraphic(null);
 }
else{
      setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
      setGraphic(paddedButton);
    }
  }
}

然后调用它:

columnName.setCellFactory(new Callback<TableColumn<YourEntity, Boolean>, TableCell<YourEntity, Boolean>>() {
          @Override public TableCell<YourEntity, Boolean> call(TableColumn<YourEntity, Boolean> BooleanTableColumn) {
            Stage stage = null;
            return new AddImage(stage, yourTableviewName);
          }
        });