How to convert IconImage to Image?

Werner Lehmann lehmann at
Wed Apr 25 06:30:57 PDT 2012


I have to use a javax.swing.Action to configure a 
javafx.scene.control.MenuItem. The problem is: how to convert the 
javax.swing.Icon into a Node which I can use as graphic for the menuitem.

Inspired by this [1] forum thread I tried the following:

>     Icon swingIcon = (Icon) action.getValue(Action.SMALL_ICON);
>     if (swingIcon instanceof ImageIcon)
>     {
>       ImageIcon swingImageIcon = (ImageIcon) swingIcon;
>       java.awt.Image awtImage = swingImageIcon.getImage();
>       Image fxImage = javafx.scene.image.Image.impl_fromExternalImage(awtImage);
>       setGraphic(new ImageView(fxImage));
>     }

Does not work: the menuitem does not show a graphic. Works ok when I use 
class.getResource, e.g.:

> Image fxImage = new javafx.scene.image.Image(ActionMenuItem.class.getResource("/help.png").toExternalForm());

So, menuitem can show a graphic, but not the graphic converted from the 
awtImage. Besides, the impl_ call is probably not the right way to do this.

According to the forum thread there will be a public Image(Object) 
constructor but I don't see it in 2.1b19. I suppose it would expose the 
same _impl method anyway and would show the same problem.

What is the best way to do this?



More information about the openjfx-dev mailing list