JavaFX basic image gallery, that allows viewing all images from specified directory.

JavaFX Basic Image Gallery
JavaFX Basic Image Gallery

Main.java

import javafx.application.Application;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.DirectoryChooser;
import javafx.stage.Stage;

import javax.imageio.ImageIO;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

// © 2018 TheFlyingKeyboard and released under MIT License
// theflyingkeyboard.net
public class Main extends Application {
    private List<Image> images = new ArrayList<>();

    @Override
    public void start(Stage primaryStage) throws Exception {
        loadImages();

        primaryStage.setWidth(1200);
        primaryStage.setHeight(800);
        primaryStage.setTitle("Image Gallery");

        ImageView mainImageView = new ImageView();
        mainImageView.setImage(images.get(0));
        mainImageView.setFitWidth(primaryStage.getWidth() - primaryStage.getWidth() / 4);
        mainImageView.setFitHeight(primaryStage.getHeight() - primaryStage.getHeight() / 4);
        mainImageView.setPreserveRatio(true);

        HBox imagesStore = new HBox(4);

        for (int i = 0; i < images.size(); ++i) {
            ImageView imageView = new ImageView();

            imageView.setOnMouseClicked(event ->
                    mainImageView.setImage(imageView.getImage())
            );

            imageView.setImage(images.get(i));
            imageView.setFitWidth(primaryStage.getWidth() / 4);
            imageView.setFitHeight(primaryStage.getHeight() / 4 - 50);
            imageView.setPreserveRatio(true);

            imagesStore.getChildren().add(imageView);
        }

        ScrollPane scrollPane = new ScrollPane(imagesStore);
        scrollPane.setVbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
        scrollPane.setMaxWidth(primaryStage.getWidth());

        BorderPane borderPane = new BorderPane();
        borderPane.setCenter(mainImageView);
        borderPane.setBottom(scrollPane);

        primaryStage.setScene(new Scene(borderPane));
        primaryStage.setResizable(false);
        primaryStage.show();
    }

    private void loadImages() {
        DirectoryChooser directoryChooser = new DirectoryChooser();
        directoryChooser.setTitle("Choose Directory");
        File directory = directoryChooser.showDialog(null);

        if (directory != null) {
            File[] files = directory.listFiles();
            String fileName;

            for (int i = 0; i < files.length; ++i) {
                fileName = files[i].getName().toLowerCase();

                if (files[i] != null && (fileName.endsWith(".jpg") || fileName.endsWith(".png") ||
                        fileName.endsWith(".bmp"))) {
                    try {
                        images.add(SwingFXUtils.toFXImage(ImageIO.read(files[i]), null));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        } else {
            System.exit(1);
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}

It could be upgraded to:

  • allow image zoom
  • load images in different thread
  • keyboard input



JavaFX Basic Image Gallery
Tagged on:                 

Leave a Reply

Your email address will not be published. Required fields are marked *

By continuing to use the site, you agree to the use of cookies. You can read more about it the Cookies&Privacy Policy Section Above. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this. You can read more about it the Cookies&Privacy Policy Section.

Close