1

I have a TilePane with a imageView items, each imageview displaying an image got from a directory in my pc. When any of the images in the tilePane is clicked a message is printed onto console with the direcory path address to the folder where the image resides, for example:

You clicked: ImageResources/wp.png

I would like to extend this further so that when a particular image is clicked the folder in which the image resides opens.

My implementation below only prints to console the message below, but no directory/ folder gets opened. The message is:

File Not Found

How can I get it to work so that the folder|directory opens? Thank you all in advance.

Also, of the directory could open with the clicked image selected, then that would be an extra, but is not a priority right now.

My implementation so far goes as follows:

public class TilePaneExample extends Application {

    @Override
    public void start(Stage primaryStage) {
        VBox root = new VBox(30);

        String[] imageResources = new String[]{
            //loading images
            "ImageResources/facebook.png",
            "ImageResources/faviicon.png",
            "ImageResources/jquery-logo.png",
            "ImageResources/linkedin_32.png",
            "ImageResources/loading1.png",
            "ImageResources/twitter.png",
            "ImageResources/twitter_32.png",
            "ImageResources/wp.png",};

        // Pane
        TilePane tilePane = new TilePane();
        tilePane.setHgap(5);
        tilePane.setVgap(5);

        for (final String imageResource : imageResources) {
            Image image = new Image(getClass().getResourceAsStream(imageResource));
            ImageView imageView = new ImageView(image);
            imageView.setOnMouseClicked(new EventHandler<MouseEvent>() {
                @Override
                public void handle(MouseEvent event) {
                    File f = new File(imageResource);
                    String absolutePath = f.getAbsolutePath();
                    String folderPath = absolutePath.
                            substring(0, absolutePath.lastIndexOf(File.separator));
                    try {
                        Desktop.getDesktop().open(new File(folderPath));
                    } catch (IllegalArgumentException iae) {
                        System.out.println("File Not Found");
                    } catch (IOException ex) {
                        Logger.getLogger(TilePaneExample.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            });
            tilePane.getChildren().add(imageView);
        }

        root.getChildren().addAll(tilePane);
        primaryStage.setTitle("TilePane Example");
        Scene scene = new Scene(root, 300, 150);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
3
  • What is the value of folderPath in runtime? Commented Aug 26, 2014 at 9:22
  • For example, clicking the second image, "ImageResources/faviicon.png", prints its folder path as D:\standAloneDev\java\workingDir\Jive\TilePaneExample\ImageResources, @Pphoenix Commented Aug 26, 2014 at 9:26
  • maybe the message of IllegalArgumentException iae shows you what the problem is. try iae.printStackTrace() instead of your sysout. Commented Aug 26, 2014 at 9:51

1 Answer 1

1

For example, clicking the second image, "ImageResources/faviicon.png", prints its folder path as D:\standAloneDev\java\workingDir\Jive\TilePaneExample\ImageResources

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.