JavaFx:在 table 单元格内显示一个图标
JavaFx : display an icon inside a table cell
我指定了以下 table 列单元格:
public class TableCellWithImage<T> extends TableCell<T, String> {
private final ImageView image;
public TableCellWithImage() {
// add ImageView as graphic to display it in addition
// to the text in the cell
image = new ImageView( new Image( getClass().getResourceAsStream("/eyes.png")));
image.setFitWidth(24);
image.setFitHeight(24);
image.setPreserveRatio(true);
setGraphic(image);
setMinHeight(70);
}
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (empty || item == null) {
// set back to look of empty cell
setText(null);
setGraphic(null);
} else {
setText(item);
setGraphic(image);
}
}
}
为了应用它,我使用
content_column_.setCellFactory(new Callback<TableColumn<DbEntry, String>, TableCell<DbEntry, String>>() {
@Override
public TableCell<DbEntry, String> call(TableColumn<DbEntry, String> param) {
return new TableCellWithImage<>();
}
});
如何对齐单元格内的图形图像和文本?我希望将图像放置在右上角,并且仅在鼠标悬停时可见。
这里有一个最终的解决方案来做一个技巧。
public class TableCellWithImage<T> extends TableCell<T, String> {
private final ImageView image;
BooleanProperty is_image_visible_ = new SimpleBooleanProperty( false );
public TableCellWithImage() {
// add ImageView as graphic to display it in addition
// to the text in the cell
image = new ImageView( new Image( getClass().getResourceAsStream("/eyes.png")));
image.setFitWidth(24);
image.setFitHeight(24);
image.setPreserveRatio(true);
setGraphic(image);
setMinHeight(70);
setGraphicTextGap(10);
setContentDisplay(ContentDisplay.RIGHT);
setOnMouseEntered(mouseEvent -> {
is_image_visible_.set(true);
});
setOnMouseExited(mouseEvent -> {
is_image_visible_.set(false);
});
image.visibleProperty().bind(is_image_visible_);
}
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (empty || item == null) {
// set back to look of empty cell
setText(null);
setGraphic(null);
} else {
setText(item);
setGraphic(image);
}
}
}
我指定了以下 table 列单元格:
public class TableCellWithImage<T> extends TableCell<T, String> {
private final ImageView image;
public TableCellWithImage() {
// add ImageView as graphic to display it in addition
// to the text in the cell
image = new ImageView( new Image( getClass().getResourceAsStream("/eyes.png")));
image.setFitWidth(24);
image.setFitHeight(24);
image.setPreserveRatio(true);
setGraphic(image);
setMinHeight(70);
}
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (empty || item == null) {
// set back to look of empty cell
setText(null);
setGraphic(null);
} else {
setText(item);
setGraphic(image);
}
}
}
为了应用它,我使用
content_column_.setCellFactory(new Callback<TableColumn<DbEntry, String>, TableCell<DbEntry, String>>() {
@Override
public TableCell<DbEntry, String> call(TableColumn<DbEntry, String> param) {
return new TableCellWithImage<>();
}
});
如何对齐单元格内的图形图像和文本?我希望将图像放置在右上角,并且仅在鼠标悬停时可见。
这里有一个最终的解决方案来做一个技巧。
public class TableCellWithImage<T> extends TableCell<T, String> {
private final ImageView image;
BooleanProperty is_image_visible_ = new SimpleBooleanProperty( false );
public TableCellWithImage() {
// add ImageView as graphic to display it in addition
// to the text in the cell
image = new ImageView( new Image( getClass().getResourceAsStream("/eyes.png")));
image.setFitWidth(24);
image.setFitHeight(24);
image.setPreserveRatio(true);
setGraphic(image);
setMinHeight(70);
setGraphicTextGap(10);
setContentDisplay(ContentDisplay.RIGHT);
setOnMouseEntered(mouseEvent -> {
is_image_visible_.set(true);
});
setOnMouseExited(mouseEvent -> {
is_image_visible_.set(false);
});
image.visibleProperty().bind(is_image_visible_);
}
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (empty || item == null) {
// set back to look of empty cell
setText(null);
setGraphic(null);
} else {
setText(item);
setGraphic(image);
}
}
}