本文介紹了JLabel:異步加載HTML圖像的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
AJLabel
允許使用HTML內容,其中可以包含一張圖片:
String html = "<html><body>...<img src="http://some_url/image.png"/>...</body></html>";
JLabel label = new JLabel(html);
請注意,我使用JLabel
在JXTreeTable
中呈現圖像,因此JLabel
文本的更新是在呈現器中的EDT上完成的。
問題是圖像是同步加載的。使用速度較慢的服務器時,EDT可能會在加載圖像時被阻止數秒。
我已經了解了圖像同步加載的原因,以及需要修改哪個類才能切換到圖像的異步加載。
圖像的加載由javax.swing.text.html.ImageView
類完成,該類有一個方法setLoadsSynchronously
。
問題是我不知道如何輕松地調整HTMLFactory
/HTMLEditorKit
,它負責創建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)
推薦答案
該圖像只是部分…的一部分不能調用
setIcon
。
一種方法是在SwingWorker
的背景中加載圖像,將其臨時保存到文件系統,并在<img/>
標記中引用保存的文件。下面的變體改編自example,是一個概念證明。您的實際實現可能使用SwingWorker<List<Row>, Row>
,其中每個Row
包含一個圖像File
;您的doInBackground()
實現將在中期結果可用時使用publish()
;您的process()
實現將確保相關樹表呈現器看到給定Row
的正確File
。
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);
}
}
}
這篇關于JLabel:異步加載HTML圖像的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,