java - 如何在 Linux 中的图像文件选择器上显示图像缩略图

标签 java swing thumbnails jfilechooser

如何使用 java 代码在 Linux(Ubuntu) 计算机中的文件选择器上显示图像缩略图。我已经尝试过一些在 Windows 平台上成功运行的代码(请参阅此链接:making jfilechooser show image thumbnails)。

public class ThumbnailFileChooser extends JFileChooser {

    private static final int ICON_SIZE = 16;
    private static final Image LOADING_IMAGE = new BufferedImage(ICON_SIZE, ICON_SIZE, BufferedImage.TYPE_INT_ARGB);
    private final Pattern imageFilePattern = Pattern.compile(".+?\\.(png|jpe?g|gif|tiff?)$", Pattern.CASE_INSENSITIVE);
    private final Map imageCache = new WeakHashMap();

    public static void main(String[] args) throws Exception {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        JFileChooser chooser = new ThumbnailFileChooser();
        chooser.showOpenDialog(null);
        System.exit(1);
    }

    public ThumbnailFileChooser() {
        super();
    }

    {
        setFileView(new ThumbnailView());
    }

    private class ThumbnailView extends FileView {

        private final ExecutorService executor = Executors.newCachedThreadPool();

        public Icon getIcon(File file) {
            if (!imageFilePattern.matcher(file.getName()).matches()) {
                return null;
            }

            synchronized (imageCache) {
                ImageIcon icon = imageCache.get(file);

                if (icon == null) {
                    icon = new ImageIcon(LOADING_IMAGE);
                    imageCache.put(file, icon);
                    executor.submit(new ThumbnailIconLoader(icon, file));
                }

                return icon;
            }
        }
    }

    private class ThumbnailIconLoader implements Runnable {

        private final ImageIcon icon;
        private final File file;

        public ThumbnailIconLoader(ImageIcon i, File f) {
            icon = i;
            file = f;
        }

        public void run() {
            System.out.println("Loading image: " + file);

            // Load and scale the image down, then replace the icon's old image with the new one.
            ImageIcon newIcon = new ImageIcon(file.getAbsolutePath());
            Image img = newIcon.getImage().getScaledInstance(ICON_SIZE, ICON_SIZE, Image.SCALE_SMOOTH);
            icon.setImage(img);

            // Repaint the dialog so we see the new icon.
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    repaint();
                }
            });
        }
    }

}

但在 Linux 中,它的工作方式与 Windows 不同。

最佳答案

您的代码在 Ubuntu 1604 上适用于我。请尝试以 root 身份运行它,以确保它不是权限问题。

关于java - 如何在 Linux 中的图像文件选择器上显示图像缩略图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40462040/

相关文章:

java - 从 swing 复制并粘贴到其他 Windows 应用程序中

jquery - 尝试创建滚动水平缩略图导航,在不使用时隐藏在左侧

c# - 从 URL 图像动态生成缩略图

Java 数组 - 不确定方法

java - 如何在数组列表中找到尖子生的名字?

java gui boxlayout问题

python - 枕头返回错误 : "IOError: image file is truncated (6 bytes not processed)"

java - 查询中的字符串参数

java - Dozer:将单个字段映射到 Set

java - JTable 自动滚动到底部