• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Can you please help me with the image URL in JavaFX?

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I saved my PNG File in sample folder,
And used that PNG file in image tag and tried to display it and worked fine.
But when I try to map it to a string in main,It shows error.
Can anybody please verify it.
I have uploaded the image to show my PNG Folder
Screen-Shot-2019-08-21-at-5.03.08-PM.png
[Thumbnail for Screen-Shot-2019-08-21-at-5.03.08-PM.png]
Here the image tag displays the image properly with no error.
Screen-Shot-2019-08-21-at-4.59.50-PM.png
[Thumbnail for Screen-Shot-2019-08-21-at-4.59.50-PM.png]
But when I try to use it here it shows error as invalid URL
Screen-Shot-2019-08-21-at-6.35.44-PM.png
[Thumbnail for Screen-Shot-2019-08-21-at-6.35.44-PM.png]
 
Marshal
Posts: 65365
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to our FX forum.

What sort of error do you get? Is that red spot a compile‑time error? What does the popup say when you hoer your mouse over the red spot? We can only help if we know the full details. Please avoid screenshots which can be difficult to read.
 
Divyadharshini Karthikeyan
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sorry for screenshots. Thought it might be easy to understand my problem with the help of images.
I get run time error from main when I retrieve the image, Whereas it loads the image properly in when I use tags and same location. Am i missing anything?
Error :
Caused by: java.lang.IllegalArgumentException: Invalid URL: Invalid URL or resource not found
at javafx.graphics/javafx.scene.image.Image.validateUrl(Image.java:1107)
at javafx.graphics/javafx.scene.image.Image.<init>(Image.java:617)
at Card/sample.Main.loadimages(Main.java:35)
at Card/sample.Main.start(Main.java:18)
 
Campbell Ritchie
Marshal
Posts: 65365
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You haven't shown us the images being loaded correctly.
Please do some debugging. What is the absolute path/URL to the images where they open normally? How does that path/URL differ from that where you are suffering the exception? What folders are you running the code from? Are the classes in the same package?
 
Divyadharshini Karthikeyan
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The folder(PNG) containing the image is on the left side of the uploaded image,to show the folder is in the same location as class.I get the error message when starting the application and my fxml is not loading because of loadimages() function in main.
 
Campbell Ritchie
Marshal
Posts: 65365
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know much FX but I do know that you get lots of problems with paths to resources. Please print the paths/URLs to those image files wherever you load them. I can't do your debugging for you.
 
Sheriff
Posts: 6163
163
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like the at sign (@) has a special meaning in an FXML URL.

https://openjfx.io/javadoc/11/javafx.fxml/javafx/fxml/doc-files/introduction_to_fxml.html#location_resolution

I'm assuming you have a directory that is literally "@PNG".  If so, you will have to change that.
 
Knute Snortum
Sheriff
Posts: 6163
163
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, it looks like from another thread you posted that you do NOT have a directory with an at sign in it.  If the at sign is handled differently in the Image class, it doesn't seem to be documented.

https://openjfx.io/javadoc/11/javafx.graphics/javafx/scene/image/Image.html

I would try to get the Image class to load your image without the at sign.  Also try loading the FXML Image without the at sign.  Try prefixing with a slash (/) or "file:".
 
Divyadharshini Karthikeyan
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 Thank you for all your suggestions.
Yes @ sign is handled differently in FXML Image.
The issue is resolved and I created a file and then used.

Solution :-

File file = new File("src/sample/PNG/" + rank[j] + suit[i] + ".png");
Image image = new Image(file.toURI().toString());

 
Ranch Hand
Posts: 201
5
MS IE Notepad Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
having "src" as part of the path looks like a wrong build config
usual you neither have src nor bin nor dist nor debug as part of the path, mostly only rsc, if at all
check your build config and your folder structer
resources like images don't go into src, thats reserved for code only, but into rsc, wich is handled different
in a correct build environment everything in src is just code wich gets compiled to either bin or dist (depend on build tool) or debug (if supported, this isn't widely used in java but more in c and c like as these languages have whats called precompilers and such wich produce different code depend on either distribution build or debug build)
if you put resources in src make sure they copied over at build time
having src in built code is wrong
 
I like tacos! And this tiny ad:
create, convert, edit or print DOC and DOCX in Java
https://products.aspose.com/words/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!