Philipp
Philipp

Reputation: 115

JavaFX Image: Check if image is valid

I have written a small JavaFX application with many configurations. One of them is, that you can define inside of a config-file which images you want to use.

Now I wanted to add error-handling for every case, but I don't know how to check, if a specific Image is drawable.

I already know, how to check for the existence of a Image, now I just want to know, how to check, if javafx would draw my Image or just do nothing.

Image img = new Image("file:corrupted.png");
gc.drawImage(img, 0, 0);

In this case, no exception is thrown and gc just draws nothing.

if (!img.isValid()) {
    throw new IllegalArgumentException;
}

I want to do something like the code above

Upvotes: 2

Views: 1295

Answers (1)

Jai
Jai

Reputation: 8363

It is possible to detect for errors during loading of Image.

From Image.errorProperty():

Indicates whether an error was detected while loading an image.

If you need to know what caused an error, you can use this:

From Image.exceptionProperty():

The exception which caused image loading to fail. Contains a non-null value only if the error property is set to true.

If your Image is constructed with a constructor overload that loads in the background, then you can also use Image.progressProperty() to check that the image has completed loading. If image.getProgress() == 1 && !image.isError() is true, then you can be sure that there is a valid image.

Upvotes: 5

Related Questions