Search code examples
imagejavafxcastingimageviewpng

How to convert WritableImage to ImageView in javaFX?


Hello Stackoverflow members, have a great day. Is there a way to convert or cast a writable image object to ImageView in order to display it in my program without the need to save the image on a hard drive.

WritableImage writable_image = barchart.snapshot(new SnapshotParameters(), null);

I need something that converts writable_image to ImaveView object.


Solution

  • You don't convert a WritableImage to an ImageView. An ImageView is a UI component designed to show an Image. Since WritableImage extends Image you don't have to do anything special.

    ImageView imageView = ...;
    WritableImage writableImage = barchart.snapshot(new SnapshotParameters(), null);
    imageView.setImage(writableImage);