Get JavaFx Path Image

I want to get the path name of the current image loaded into my Image object.

I have the following code:

Image lol = new Image("fxml/images/bilhar9.png"); 

and I want to do something like:

 lol.getPath(); 

which should return "fxml / images / bilhar9.png", I found the impl_getUrl () method, but it is deprecated.

What should I do?

+6
source share
2 answers

You cannot get the image path from the image through a non-obsolete API, because such an API does not exist in Java 8. You can use the obsolete API and the risk of breaking your application in a future version of Java when the obsolete API is removed - this is not recommended. You can create a function request to make getURL () the public API on the image, but there is no guarantee that it will be accepted, and even if it was, it would only turn it into a later release of Java.

The image is not final, so I suggest the following:

 class LocatedImage extends Image { private final String url; public LocatedImage(String url) { super(url); this.url = url; } public String getURL() { return url; } } 

Create your image as follows:

 Image image = new LocatedImage("fxml/images/bilhar9.png"); 

You can then access the URL through:

 String url = image instanceof LocatedImage ? ((LocatedImage) image).getURL() : null; 

Not a great solution, but may be enough for your case.

+12
source

img (). impl_getUrl () returns the string path

0
source

Source: https://habr.com/ru/post/973327/


All Articles