I have a GIF image file that I was displaying in an ImageView with JavaFX. I am allowing administrators to change the logo file and I want to ensure it scales correctly to fit the display area. I had it all working until I had a file that was a bit taller than wide and taller than the fitHeight.
When the file is not a happy size, the image does NOT display. I'd like some suggestions for a fall-back strategy. My preferred options are:
- Detect if the image will fail-to-display after the ImageView.setImage() method
- I can then reload the image and change the fitHeight to a known safe value and issue a warning, or
- Force the image to display so that it is clipped to the fitWidth and fitHeight
- Keep the previous image if the file is incompatible
- Reuse the previous image by saving: this.logo.getImage()
- Trick question: How do I know the image does not display / will be incompatible?
- I wondered if there's an event that I can check to verify/fix an image load?
I'm open to alternative strategy suggestions. For a bit of context the logo image is specified by a URL via a config file and an FXML that is loaded at (unattended) boot time. We can't have a blank box on-screen something needs to be done if there's a problem displaying an image.
I can't identify a JavaFX call to inform me if the image is actually visible after the setImage() call. To be fair the scene refresh happens after this.
I have identified the source of the The following the top line works:
<ImageView fx:id="logo" fitHeight="320.0" fitWidth="600.0" pickOnBounds="true" preserveRatio="true" GridPane.halignment="CENTER" GridPane.valignment="CENTER">
This version fails to display:
<ImageView fx:id="logo" fitHeight="360.0" fitWidth="600.0" pickOnBounds="true" preserveRatio="true" GridPane.halignment="CENTER" GridPane.valignment="CENTER">
fitHeight values under about 350 work, determined by experimentation. When fitHeight is too high, the image is invisible (not displayed).
possibly related: