I am creating image gallery using javafx. I found many things on internet regarding this but not able to get any suitable help for this issue. I have to create one image gallery like picasa viewer. all the images is in thumbnail view in my image view and after that when I select image that is in pop-up viewer. I did it some code for that but I didn't get proper output. All the images from the folder redraw from the same co-ordinates. Below is my code and output.
@Override
public void initialize(URL url, ResourceBundle rb) {
String path = "/home/ubuntu/eclipse with liferay/Desktop/imagetest/";
File folder = new File(path);
File[] listOfFiles = folder.listFiles();
for (final File file : listOfFiles) {
ImageView imageView;
imageView = createImageView(file);
imagecontainer.getChildren().addAll(imageView);
}
}
private ImageView createImageView(final File imageFile) throws FileNotFoundException, FileNotFoundException, FileNotFoundException, FileNotFoundException {
// DEFAULT_THUMBNAIL_WIDTH is a constant you need to define
// The last two arguments are: preserveRatio, and use smooth (slower) resizing
ImageView imageView = null;
try {
final Image image;
image = new Image(new FileInputStream(imageFile), DEFAULT_THUMBNAIL_WIDTH, 0, true, true);
imageView = new ImageView(image);
} catch (FileNotFoundException ex) {
Logger.getLogger(GalleryController.class.getName()).log(Level.SEVERE, null, ex);
}
return imageView;
}
}
kindly help me to resolve my issue. I want to display images one by one as thumbnail view.
You need to create a TilePane and add the ImageView's to it. You can have a ScrollPane if needed. A complete example with double click to create a fullscreen preview is shown below. You can of course do necessary changes for creating a FXML :)
Output
This works great when you resize the Window, making the ScrollPane visible, if required.