How to convert IconImage to Image?

Werner Lehmann lehmann at media-interactive.de
Thu Apr 26 08:30:58 PDT 2012


I have found a workaround to get from swing.ImageIcon to fx.Image: the 
ImageIcon description can be an external URL form. Dirty as can be, of 
course...

> ImageIcon swingImageIcon = (ImageIcon) swingIcon;
> Image fxImage = new javafx.scene.image.Image(swingImageIcon.getDescription());
> setGraphic(new ImageView(fxImage));

Werner

On 25.04.2012 15:30, Werner Lehmann wrote:
> Hi,
>
> 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?
>
> Rgds
> Werner
>
>
>
> [1] https://forums.oracle.com/forums/thread.jspa?threadID=2363008


More information about the openjfx-dev mailing list