I have a TableView
that lists files within a given folder. Within the TableView, I have a column that holds a button which opens the file when clicked.
I am trying to get the file's filetype icon to display as that button's graphic. For example, a .xlsx
file would display the Microsoft Excel icon and .pdf
files show the Adobe PDF icon.
From my research, I understand JavaFX has no native way to get a file's associated icon so I will need to do some heavy-handed Swing conversions.
However, the I cannot figure out how to do so within a CellFactory
.
Here is the partial code I have so far, in which I need to add the code to get the file's icon:
colOpenFile.setCellFactory(col -> {
final javafx.scene.control.Button btnOpen = new Button();
final ImageView openIcon;
TableCell<FileResource, FileResource> cell = new TableCell<FileResource, FileResource>() {
@Override
protected void updateItem(FileResource item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setGraphic(null);
setText(null);
} else {
Icon icon = FileSystemView.getFileSystemView().getSystemIcon(new File(cell.getItem().getFilename()));
setAlignment(Pos.CENTER);
openIcon = fetchFileIcon(item.getFilename());
openIcon.setFitWidth(16);
openIcon.setFitHeight(16);
btnOpen.setGraphic(openIcon);
setGraphic(btnOpen);
}
}
};
}
I have not finished attempting this implementation because I already know this throws an Exception
because cell
may not have been initialized.
How do I get the file icon for the FileResource
being displayed on that row and assign it to the Button
, also in that row?
Thank you!