Java / JavaFX:为JavaFX标签设置Swing图标 [英] Java/JavaFX: Set Swing Icon for JavaFX label

查看:1441
本文介绍了Java / JavaFX:为JavaFX标签设置Swing图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从文件(.ico / .exe)中读取缩略图(图标; 32x32px)并将其设置为JavaFX标签。

I'm trying to read a thumbnail (icon; 32x32px) from a file (.ico/.exe) and set it to a JavaFX label.

我的第一次尝试:

public Icon getLargeIcon(String exeFile) {
    if (exeFile != null) {
        File file = new File(exeFile);
        try {
            ShellFolder sf = ShellFolder.getShellFolder(file);
            return new ImageIcon(sf.getIcon(true), sf.getFolderType());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
    return null;
}

之后我这样做:

    Icon largeIcon = getLargeIcon(file.getAbsolutePath());
    ImageIcon swingImageIcon = (ImageIcon) largeIcon;
    java.awt.Image awtImage = swingImageIcon.getImage();
    Image fxImage = javafx.scene.image.Image.impl_fromPlatformImage(awtImage);
    lblAppIconValue.setGraphic(new ImageView(fxImage));

我通过多个网站搜索并找到了这个,但它给了我一个例外:
java.lang.UnsupportedOperationException:loadPlatformImage不支持的类

I've searched trough several sites and found this, but it gives me an exception: java.lang.UnsupportedOperationException: unsupported class for loadPlatformImage

我的第二次尝试:

            URL url = file.toURI().toURL();
            Image image = new Image(url.toString());
            lblAppIconValue.setGraphic(new ImageView(image));

也不行......

我的问题:如何将javax.swing.Icon设置为JavaFX标签?可能吗?如果不可能,我如何从文件中读取缩略图并将其设置为JavaFX标签的图标/图形?

My question: How can I set a javax.swing.Icon to a JavaFX label? Is it possible? If it's not possible, how can I read a thumbnail from a file and set it as an icon/graphic for a JavaFX label?

谢谢!

推荐答案

永远不要使用 impl _ 方法:这些不是公共API的一部分。

Never use impl_ methods: these are not part of the public API.

要将awt图像转换为FX图像, javafx.embed.swing中的 SwingFXUtils 有一个 toFXImage(...)方法,它将 BufferedImage 转换为JavaFX 图片。目前尚不清楚图标中的图像是否为 BufferedImage ,因此您需要执行以下几个步骤:

To convert an awt Image to an FX Image, the SwingFXUtils class in javafx.embed.swing has a toFXImage(...) method that converts a BufferedImage to a JavaFX Image. It's not clear whether the image you have from the icon is a BufferedImage, so you'll need a couple of steps to make that work:

BufferedImage bImg ;
if (awtImage instanceof BufferedImage) {
    bImg = (BufferedImage) awtImage ;
} else {
    bImg = new BufferedImage(awtImage.getWidth(null), awtImage.getHeight(null), BufferedImage.TYPE_INT_ARGB);
    Graphics2D graphics = bImg.createGraphics();
    graphics.drawImage(awtImage, 0, 0, null);
    graphics.dispose();
}
Image fxImage = SwingFXUtils.toFXImage(bImg, null);

这是一种相当低效的方法,因为您首先从文件创建awt图像,然后转换它可能通过中间缓冲图像到FX图像。如果您有权访问 ShellFolder 类的源代码,您可能会看到如何实现 getIcon()方法并按照相同的过程。在某些时候,它必须得到一个带有图像数据的 InputStream ;一旦你有了,你可以将它传递给 javafx.scene.image.Image 构造函数。

This is a fairly inefficient approach, as you are first creating an awt image from your file, then converting it to an FX image, possibly via an intermediate buffered image. If you have access to the source code for the ShellFolder class, you might see how that implements the getIcon() method and follow the same process. At some point, it must get an InputStream with the image data; once you have that you can pass it to the javafx.scene.image.Image constructor.

这篇关于Java / JavaFX:为JavaFX标签设置Swing图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆