Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What has happened to SwingFXUtils?

I just started using JavaFX 11, and SwingFXUtils seems to be gone (or renamed?)

I tried to download it manually, but the module-system complained about import sun.awt.image.IntegerComponentRaster;, and I couldn't get it to work.

I need to convert Swing Icon to JavaFX image.

like image 909
user38725 Avatar asked Sep 28 '18 15:09

user38725


2 Answers

Since Java 9, SwingFXUtils has been moved to the javafx.swing module, under the package javafx.embed.swing.

See JavaFX 9 Javadoc and the new JavaFX 11 Javadoc, hosted at https://openjfx.io.

If you have any issue with your sample, make sure you are adding the proper VM options. See the samples from the getting started guide.

Probably you will need to add something like:

--module-path <path-to>/javafx-sdk-11/lib --add-modules=javafx.controls,javafx.swing
like image 126
José Pereda Avatar answered Nov 09 '22 04:11

José Pereda


SwingFXUtils is still there if you have correctly added the javafx.swing module to your build path (see answer from José) but importing an internal class like sun.awt.image.IntegerComponentRaster is not allowed anymore.

To convert your icon you can try this:

Icon icon; // Your icon
BufferedImage image = (BufferedImage)((ImageIcon)icon).getImage();
WritableImage writable = SwingFXUtils.toFXImage(image, null);

Whether this works or not depends on how your icon was created but it is worth a try.

like image 31
mipa Avatar answered Nov 09 '22 02:11

mipa