Home > Net >  JavaFX doesn't read images - how to reduce code, which is responsible for showing the images
JavaFX doesn't read images - how to reduce code, which is responsible for showing the images

Time:04-13

Im currently working on javafx app. I have problem with images - when added in scene builder, they arent visible when the app is run (they can be seen when clicking show preview in window in scene builder) I have solution for that, which isnt very great but at least it works (if you know something better then please let me know)

I have to add this code in initialize method for every image I have on current fxml file

File facebookFile = new File("resources/facebook.png");
Image facebookImage = new Image(facebookFile.toURI().toString());
facebookImageView.setImage(facebookImage);

I was trying to figure out solution, where I wont have to repeat those lines for every imageView. I thought about a funcion, but i cant finish it, cuz I cant figure out a way to setImage of created file (last line of code)

 private void setImages(String imageName) {
        String path = "resources/"   imageName;
        String imageViewName = imageName   "ImageView";
        File file = new File(path);
        Image image = new Image(file.toURI().toString());
        // perfectly there would be something like (ImageView) path.setImage(image)

    }

If you have any idea, please let me know.

CodePudding user response:

I have just thought that i can add one more paramether to my method, so it looks like this:

 private void setImages(ImageView imageView, String imageName) {
        String path = "resources/"   imageName;
        String imageViewName = imageName   "ImageView";
        File file = new File(path);
        Image image = new Image(file.toURI().toString());
        imageView.setImage(image);

    }

and everything should work - but there is one more think - I have added jfoenix jar to library and now i dont need any of these. I dont know why it worked after some time (I was checking it when I added this jar to my library) but it works:P

CodePudding user response:

maybe something like:

Image image = new Image(new File("yourImg.gif").toURI().toString());
ImageView imageview = new ImageView(image);
  • Related