File Tree View with Image
Having had the message on my blog about JavaFX TreeViewSample(FYR. 2013-03-23 - tomoTakaの日記), I was so happy. So I wanted to add ImageView on this application.
It is easy thanks to JavaFX ImageView control.
- Figure1
You can see the Images on each Tree Cell.
- code1(PathTreeCell.java)
This code shows you how to set ImageView to TreeCell by using setGraphic method.
@Override protected void updateItem(PathItem item, boolean empty) { super.updateItem(item, empty); if (empty) { setText(null); setGraphic(null); } else { final String itemString = getString(); if (isEditing()) { if (textField != null) { textField.setText(itemString); } setText(null); setGraphic(textField); } else { setText(itemString); if (isImage(itemString)) { // <-- see code2 setGraphic(getImage(itemString)); // <-- *** add ImageView here(see code3) } else { setGraphic(null); } if (!getTreeItem().isLeaf()) { setContextMenu(dirMenu); } else { setContextMenu(fileMenu); } } } }
- code 2
I found the blog about how to check if the file is image or not.(http://qiita.com/pcpDev/items/a4963bb2a7c3e5073cdb).
This helps me a lot, but it does not work for tiff file...
... private static List<String> imageList; static { imageList = Arrays.asList("BMP", "DIB", "RLE", "JPG", "JPEG" , "JPE", "JFIF" , "GIF", "EMF", "WMF", "TIF", "TIFF", "PNG", "ICO"); } ... private boolean isImage(String itemPath) { String parentPath = getItem().getPath().getParent().toAbsolutePath().toString(); try { URL url = new URL("file:" + parentPath + "/" + itemPath); String formatName = ""; try (ImageInputStream iis = ImageIO.createImageInputStream(url.openStream())) { Iterator<ImageReader> readers = ImageIO.getImageReaders(iis); if (readers.hasNext()) { // <-- this does not work for tiff file??? ImageReader reader = readers.next(); formatName = reader.getFormatName(); } } if (imageList.contains(formatName.toUpperCase())) { return true; } } catch (Exception e) { return false; } return false; }
- code 3
ImageView has very useful method like setFitWidth to resize your image.
private Node getImage(String itemPath) { String parentPath = getItem().getPath().getParent().toAbsolutePath().toString(); Image image = new Image("file:" + parentPath + "/" + itemPath); ImageView imageView = new ImageView(); imageView.setImage(image); imageView.setFitWidth(10); // <-- set size of image imageView.setPreserveRatio(true); return imageView; }
The whole code is here(tomoTaka01/FileTreeViewSample: JavaFX File ... - GitHub)
PS; When I put this blog,Mr.@skrb who is Java champion in Japan had suggested to use javafx.scene.image.Image instead of ImageIO. Then I amended the code about how to check if TreeCell is image or not. I feel so happy since I received message from such a nice person!
- amended code1
... setText(itemString); final Node image = getImage(itemString); if (image != null) { // <-- not use isImage method(code2) setGraphic(image); // <-- *** add image } else { setGraphic(null); } ...
- amended code3
private Node getImage(String itemPath) { String parentPath = getItem().getPath().getParent().toAbsolutePath().toString(); Image image = new Image("file:" + parentPath + "/" + itemPath); if (image.isError()) { return null; // <-- if not image } ImageView imageView = new ImageView(); imageView.setImage(image); imageView.setFitWidth(10); // <-- set size of image imageView.setPreserveRatio(true); return imageView; }
I tried to resolve about Tiff file, but it is very difficult for me so far. I will find the way how to deal Tiff file in Java...
Anyway coding something in Java is so fun. ;-)