JLabel:异步加载HTML图像

时间:2017-07-06 08:57:29

标签: java swing jlabel

JLabel允许HTML内容,其中可以包含内容中的图像:

String html = "<html><body>...<img src=\"http://some_url/image.png\"/>...</body></html>";
JLabel label = new JLabel(html);

请注意,我使用JLabelJXTreeTable中渲染图片,因此JLabel文本的更新是在渲染器的EDT上完成的。

问题是图像是同步加载的。使用慢速服务器时,EDT可能会在加载图像时被阻止多秒钟。

我已经发现了为什么图像会同步加载,以及我需要修改哪个类来切换到图像的异步加载。

图片的加载由javax.swing.text.html.ImageView类完成,该类的方法为setLoadsSynchronously

问题在于我不知道如何轻松调整负责创建HTMLFactory的{​​{1}} / HTMLEditorKit,以及{{1}内部使用的ImageView / JLabel }。 为了使事情变得更加复杂,我需要一个适用于所有外观和感觉的解决方案。

如果上述情况不明确,则以下线程转储显示在图像检索期间EDT被阻止的内容:

"AWT-EventQueue-0@999" prio=6 tid=0x10 nid=NA waiting
  java.lang.Thread.State: WAITING
      at java.lang.Object.wait(Object.java:-1)
      at java.awt.MediaTracker.waitForID(MediaTracker.java:677)
      at javax.swing.ImageIcon.loadImage(ImageIcon.java:314)
      at javax.swing.ImageIcon.setImage(ImageIcon.java:381)
      at javax.swing.text.html.ImageView.loadImage(ImageView.java:704)
      at javax.swing.text.html.ImageView.refreshImage(ImageView.java:673)
      at javax.swing.text.html.ImageView.sync(ImageView.java:645)
      at javax.swing.text.html.ImageView.getPreferredSpan(ImageView.java:443)
      at javax.swing.text.FlowView$LogicalView.getPreferredSpan(FlowView.java:732)
      at javax.swing.text.FlowView.calculateMinorAxisRequirements(FlowView.java:233)
      at javax.swing.text.ParagraphView.calculateMinorAxisRequirements(ParagraphView.java:717)
      at javax.swing.text.html.ParagraphView.calculateMinorAxisRequirements(ParagraphView.java:157)
      at javax.swing.text.BoxView.checkRequests(BoxView.java:935)
      at javax.swing.text.BoxView.getMinimumSpan(BoxView.java:568)
      at javax.swing.text.html.ParagraphView.getMinimumSpan(ParagraphView.java:270)
      at javax.swing.text.BoxView.calculateMinorAxisRequirements(BoxView.java:903)
      at javax.swing.text.html.BlockView.calculateMinorAxisRequirements(BlockView.java:146)
      at javax.swing.text.BoxView.checkRequests(BoxView.java:935)
      at javax.swing.text.BoxView.getMinimumSpan(BoxView.java:568)
      at javax.swing.text.html.BlockView.getMinimumSpan(BlockView.java:378)
      at javax.swing.text.BoxView.calculateMinorAxisRequirements(BoxView.java:903)
      at javax.swing.text.html.BlockView.calculateMinorAxisRequirements(BlockView.java:146)
      at javax.swing.text.BoxView.checkRequests(BoxView.java:935)
      at javax.swing.text.BoxView.getMinimumSpan(BoxView.java:568)
      at javax.swing.text.html.BlockView.getMinimumSpan(BlockView.java:378)
      at javax.swing.text.BoxView.calculateMinorAxisRequirements(BoxView.java:903)
      at javax.swing.text.html.BlockView.calculateMinorAxisRequirements(BlockView.java:146)
      at javax.swing.text.BoxView.checkRequests(BoxView.java:935)
      at javax.swing.text.BoxView.getPreferredSpan(BoxView.java:545)
      at javax.swing.text.html.BlockView.getPreferredSpan(BlockView.java:362)
      at javax.swing.plaf.basic.BasicHTML$Renderer.<init>(BasicHTML.java:383)
      at javax.swing.plaf.basic.BasicHTML.createHTMLView(BasicHTML.java:67)
      at javax.swing.plaf.basic.BasicHTML.updateRenderer(BasicHTML.java:207)
      at javax.swing.plaf.basic.BasicLabelUI.propertyChange(BasicLabelUI.java:417)
      at javax.swing.plaf.synth.SynthLabelUI.propertyChange(SynthLabelUI.java:296)
      at java.beans.PropertyChangeSupport.fire(PropertyChangeSupport.java:335)
      at java.beans.PropertyChangeSupport.firePropertyChange(PropertyChangeSupport.java:327)
      at java.beans.PropertyChangeSupport.firePropertyChange(PropertyChangeSupport.java:263)
      at java.awt.Component.firePropertyChange(Component.java:8428)
      at org.jdesktop.swingx.renderer.JRendererLabel.firePropertyChange(JRendererLabel.java:292)
      at javax.swing.JLabel.setText(JLabel.java:330)

2 个答案:

答案 0 :(得分:1)

  

图片只是HTML的部分 ...调用setIcon不是一个选项。

一种方法是将图像加载到SwingWorker的背景中,将其临时保存到文件系统,并在<img/>标记中引用保存的文件。根据此example改编的以下变体是概念证明。您的实际实施可能会使用SwingWorker<List<Row>, Row>,其中每个Row都包含图片File;您的doInBackground()实施将在publish()临时结果可用时生效;您process()的实施将确保相关的树表格渲染器看到给定File的正确Row

import java.awt.*;
import java.awt.image.*;
import java.io.*;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;

/**
 * @see http://stackoverflow.com/questions/4530659 */
public class WorkerTest extends JFrame {

    private JPanel panel = new JPanel();
    private JLabel label = new JLabel("Loading...");

    public WorkerTest() {
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        label.setHorizontalTextPosition(JLabel.CENTER);
        label.setVerticalTextPosition(JLabel.CENTER);
        this.add(label);
        this.pack();
        this.setLocationRelativeTo(null);
    }

    private void start() {
        new ImageWorker().execute();
    }

    public static void main(String args[]) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                WorkerTest wt = new WorkerTest();
                wt.setVisible(true);
                wt.start();
            }
        });
    }

    class ImageWorker extends SwingWorker<File, Void> {

        private static final String TEST =
            "http://cdn.sstatic.net/stackexchange/img/logos/so/so-logo.png";
        private BufferedImage image;
        private File file;

        @Override
        protected File doInBackground() throws IOException {
            image = ImageIO.read(new URL(TEST));
            file = File.createTempFile("image", null);
            ImageIO.write(image, "png", file);
            return file;
        }

        @Override
        protected void done() {
            label.setText("<html><body><img src=\"file://"
                + file.getAbsolutePath() + "\"/></body></html>");
            panel.setPreferredSize(new Dimension(image.getWidth(), image.getHeight()));
            WorkerTest.this.pack();
            WorkerTest.this.setLocationRelativeTo(null);
        }
    }
}

答案 1 :(得分:0)

使用SwingWorkers(由transshgod建议)是最好的解决方案。另一种选择是这样的: 创建一个卸载图像的异步方法:

new Thread(new Runnable() {
    public void run() {
       //download the image
       BufferedImage image = ImageIO.read(url);
       //update the label
       updateLabel(image)
    }
}).start();

调用另一种更新EDT中标签的方法

private void updateLabel(final BufferedImage image) {
  SwingUtilities.invokeLater(new Runnable() {
    public void run(){
      // fine updating here... event dispatch thread
      label.setIcon(new ImageIcon(image));
    }
  });
}