0

I have images listed in a TilePane, each, when clicked, should print out its file path. I however, get something like:

javafx.scene.image.Image@1a3d58b

How can I get the path to print out in a normal format, something like D:\Xampp\

Thank you.

Here's the Class:

public class FlowTileExample extends Application {

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

        //loading images
        Image[] im = new Image[8];
        im[0] = new Image(getClass().getResourceAsStream("facebook.png"));
        im[1] = new Image(getClass().getResourceAsStream("faviicon.png"));
        im[2] = new Image(getClass().getResourceAsStream("jquery-logo.png"));
        im[3] = new Image(getClass().getResourceAsStream("linkedin_32.png"));
        im[4] = new Image(getClass().getResourceAsStream("loading1.png"));
        im[5] = new Image(getClass().getResourceAsStream("twitter.png"));
        im[6] = new Image(getClass().getResourceAsStream("twitter_32.png"));
        im[7] = new Image(getClass().getResourceAsStream("wp.png"));

        VBox up = new VBox(20);
        Text text4flow = new Text("Images in FlowPane");
        text4flow.setFont(Font.font("Calibri", FontWeight.BOLD, 30));
        text4flow.setUnderline(true);
        VBox.setMargin(text4flow, new Insets(10, 0, 0, 10));
        //creating Flow Pane
        FlowPane flowpane = new FlowPane();
        flowpane.setHgap(5);
        flowpane.setVgap(5);

        EventHandler mouseHandler = new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent t) {
                ImageView imageView = (ImageView) t.getSource();
                System.out.println("You clicked " + imageView.getImage());
            }
        };

        for (int i = 0; i < 8; i++) {
            ImageView imageView = new ImageView(im[i]);
            imageView.setOnMouseClicked(mouseHandler);
            flowpane.getChildren().add(imageView);
        }

        up.getChildren().addAll(text4flow, flowpane);

        VBox down = new VBox(20);
        Text text4tile = new Text("Images in TilePane");
        text4tile.setFont(Font.font("Calibri", FontWeight.BOLD, 30));
        text4tile.setUnderline(true);
        VBox.setMargin(text4tile, new Insets(10, 0, 0, 10));

        //creating Tile Pane
        TilePane tilepane = new TilePane();
        tilepane.setHgap(5);
        flowpane.setVgap(5);
        for (int i = 0; i < 8; i++) {
            tilepane.getChildren().add(new ImageView(im[i]));
        }
        down.getChildren().addAll(text4tile, tilepane);
        root.getChildren().addAll(up, down);
        primaryStage.setTitle("Flow And Tile Panes Example");
        Scene scene = new Scene(root, 500, 600);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
  • This can't be done the way you're doing it. The `Image` object doesn't have this information, and you're loading the image via the `ClassLoader` anyway so you can't even store the file path in a lookup table to use later. – durron597 May 14 '14 at 13:18
  • Hi [durron597](http://stackoverflow.com/users/1768232/durron597). Thanks for the reply. Would you mind pointing me on how to go about it? – user3625142 May 14 '14 at 13:25
  • You already have the "path names" (really resource names) in your code. You just need to keep references to them and use them in the event handler. – James_D May 14 '14 at 15:48

2 Answers2

1

@durron597 is correct, the Image object doesn't have the Path details of the file from where the image is getting loaded, and it should not !

If you are using it for a demo proect, or a small project, which you do not consider of using/upgrading in the future, here is a small trick for you.

Try using

imageView.getImage().impl_getUrl()

this will return you

file:<your file path>

So in order to filter it, you can go with

imageView.getImage().impl_getUrl().substring(5)
Community
  • 1
  • 1
ItachiUchiha
  • 36,135
  • 10
  • 122
  • 176
  • Thank you so much [@ItachiUchiha](http://stackoverflow.com/users/1759128/itachiuchiha) for the reply and the nice answer. Let me try it out. – user3625142 May 14 '14 at 13:29
  • @durron597 Yes I know, and you are correct. I missed the warning while posting the answer. I have added it for future references ! – ItachiUchiha May 14 '14 at 15:06
  • You really should not use `impl_*` methods under any circumstances. They are public only because the private API needs to be able to access them, and should not be considered part of the public API. They are marked deprecated to encourage you not to use them and are deliberately omitted from the API documentation. There is no guarantee they will be present even in a minor update of the JRE. – James_D May 14 '14 at 15:25
  • @James_D Is there any other way of getting the image path from the Image object itself ? – ItachiUchiha May 14 '14 at 15:41
  • No. In general, the Image doesn't even necessarily come from a file or other resource; it might be a dynamically created WritableImage, for example. Since here the OP is working directly with the names of the image resources, all that's needed is to work with those in the event handler too. – James_D May 14 '14 at 15:44
0

You could create a class encapsulating the information you have, along with the image, and then just refer back to instances of that class. Or, more simply, why not just do:

String[] imageResources = new String[] {
    "facebook.png",
    "faviicon.png", 
    // etc
};

// ...

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) {
            System.out.println("You clicked: "+imageResource);
        }
    });
    flowPane.getChildren().add(imageView);
}
James_D
  • 201,275
  • 16
  • 291
  • 322