Pergunta

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);
    }
}
Foi útil?

Solução 2

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);
}

Outras dicas

@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)
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top