0

Pretty new to JavaFX and I'm having some trouble displaying an image after runtime. This is the method that is causing me trouble.

  private void lookUp(Site site) {

    String link = site.getLink();

    String imageName = link.replace("/", "1");
    imageName = imageName.replace(".", "4");
    imageName = imageName.replace(":", "3");


    if(site.getImagePath() != null){
        //what to do if this shit does have an image already
        System.out.println("sample/Resources/images/" + site.getImagePath() + ".jpg");
        String imagePath = "/sample/Resources/images/" + site.getImagePath() + ".jpg";
        imagePath.trim();



        System.out.println(site.getImagePath());
        System.out.println(System.getProperty("user.dir"));
        Image image = new Image(imagePath);

        webShot.setImage(image);

    }

    else{
        //What to do if the image doesn't exist
         Snapshot service = new Snapshot();
         service.setLink(site.getLink());

         service.setOnSucceeded(t -> {
             System.out.println("done:" + t.getSource().getValue());
             site.setImagePath(t.getSource().getValue().toString());
             progressToScreenShot.setProgress(100);
             progressToScreenShot.setOpacity(0);

             Image image = new Image("src/sample/Resources/images/" + site.getImagePath() + ".jpg");
             webShot.setImage(image);

         });

         service.setOnRunning(t -> {
             progressToScreenShot.setOpacity(1.0);
             progressToScreenShot.setProgress(service.getProgress());
         });

         service.start();



    }//End of Else

}

The odd thing is on a second run where the image has saved to folder it'll appear in the menu. The service returns a snapshot of a website using an API called grabItz. Error given is URL Caused by: java.lang.IllegalArgumentException: Invalid URL or resource not found

But if it was an invalid URL or the resource wasn't there how does it appear on a second run through??

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Kavin Jey
  • 9
  • 1
  • 1
    Does the println display the same path each time? – VGR Jan 03 '18 at 17:37
  • @VGR yes same path is displayed each time – Kavin Jey Jan 03 '18 at 18:10
  • If you do save it as a file in your source folder probably it only gets added to the classpath after rebuilding... Try [using the URL of the file](https://stackoverflow.com/questions/6098472/pass-a-local-file-in-to-url-in-java) you save the image to. – fabian Jan 03 '18 at 18:43
  • @fabian Do you mean something like this, this implementation doesn't work either and gives the same error: String concatLink = "file:sample/Resources/images/" + site.getImagePath() + ".jpg"; URL imagePath = new File(concatLink).toURI().toURL(); Image image = new Image(imagePath.getPath()); webShot.setImage(image); – Kavin Jey Jan 03 '18 at 19:20

0 Answers0