javafx - JavaFx:在表格单元格内显示一个图标
问题描述
我指定了下表列单元格:
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);
}
}
}
推荐阅读
- flutter - Flutter Randomizing - 如何防止显示之前显示的元素
- javascript - 在 Vue 中播放声音 onClick 事件返回 Uncaught (in promise) DOMException: Failed to load because no supported source was found
- mysql - 存储在 mysql 数据库中的日期与客户端和服务器中显示的日期不同,在 localhost 上运行
- javascript - 将数组拆分为两部分
- scala - 数组构造函数优化——Double的装箱
- sql - Oracle Select 语句中的工资差异
- python - 关于 Flutter 和 Python 之间的本地连接
- c# - 如何在 ASP Core 的静态类中使用“IWebHostEnvironment”
- ada - 为什么 GNAT 会拒绝具有默认判别值的数组类型?
- javascript - 在构建电子时如何提供自己的版本