JavaFx - 为什么不能覆盖方法 "updateItem"?
JavaFx - why can't Override the method "updateItem"?
我尝试将图像添加到 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);
}
你为什么不试试这样简单又高效的方法:
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);
}
});
您可以使用以下细胞工厂 -
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; }});
我尝试将图像添加到 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);
}
你为什么不试试这样简单又高效的方法:
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);
}
});
您可以使用以下细胞工厂 -
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; }});