简体   繁体   中英

JavaFx - why can't Override the method “updateItem”?

I try to add Image to cells in TabelView. I read some solutions (like this: Placing an image into a JavaFX 2 table ), all of them have to do override the method "updateItem" but it doesn't exist for me. What is my mistake?

Here the code:

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

You can use the following cell factory -

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

Why wont you try something like this , easy and efficient :

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

and just call it :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM