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??

4
  • 1
    Does the println display the same path each time? Commented Jan 3, 2018 at 17:37
  • @VGR yes same path is displayed each time Commented Jan 3, 2018 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 you save the image to. Commented Jan 3, 2018 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); Commented Jan 3, 2018 at 19:20

0

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.