2021年12月2日 | Leave a comment 问题: JTable 如何后台加载图片 解决: package yvhk.t3dcool.admin.appclient.model; import javax.swing.*; import javax.swing.table.DefaultTableCellRenderer; import java.awt.*; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; import java.util.HashMap; import java.util.Map; class ImgCellRender extends DefaultTableCellRenderer { Map<String, byte[]> imgCache = new HashMap<>(100); public void clearCache() { imgCache.clear(); } @Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { var url = UrlHelper.getDisplayImageUrl((String) value, "s150"); //not work var ret = new JLabel("hello"); var imgData = imgCache.get(url); ImageIcon icon; if (imgData == null) { var worker = new ImgLoaderWorker(url, () -> { var rect = table.getCellRect(row, column, true); table.repaint(rect); }); worker.execute(); icon = null; } else { icon = new ImageIcon(Toolkit.getDefaultToolkit().createImage(imgData)); } setIcon(icon); return this; } class ImgLoaderWorker extends SwingWorker<byte[], Void> { final String url; private final Runnable doneHandle; ImgLoaderWorker(String url, Runnable doneHandle) { this.url = url; this.doneHandle = doneHandle; } @Override protected byte[] doInBackground() throws Exception { var ret = img(url); return ret; } @Override protected void done() { try { imgCache.put(url, get()); } catch (Exception e) { System.err.println("加载图片出错"); } if (doneHandle != null) { doneHandle.run(); } } byte[] img(String url) throws IOException, InterruptedException, URISyntaxException { HttpRequest req = HttpRequest.newBuilder(new URI(url)) .GET().build(); var resp = HttpClient.newHttpClient() .send(req, HttpResponse.BodyHandlers.ofByteArray()); var ret = resp.body(); return ret; } } } 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 package yvhk.t3dcool.admin.appclient.model; import javax.swing.*;import javax.swing.table.DefaultTableCellRenderer;import java.awt.*;import java.io.IOException;import java.net.URI;import java.net.URISyntaxException;import java.net.http.HttpClient;import java.net.http.HttpRequest;import java.net.http.HttpResponse;import java.util.HashMap;import java.util.Map; class ImgCellRender extends DefaultTableCellRenderer { Map<String, byte[]> imgCache = new HashMap<>(100); public void clearCache() { imgCache.clear(); } @Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { var url = UrlHelper.getDisplayImageUrl((String) value, "s150"); //not work var ret = new JLabel("hello"); var imgData = imgCache.get(url); ImageIcon icon; if (imgData == null) { var worker = new ImgLoaderWorker(url, () -> { var rect = table.getCellRect(row, column, true); table.repaint(rect); }); worker.execute(); icon = null; } else { icon = new ImageIcon(Toolkit.getDefaultToolkit().createImage(imgData)); } setIcon(icon); return this; } class ImgLoaderWorker extends SwingWorker<byte[], Void> { final String url; private final Runnable doneHandle; ImgLoaderWorker(String url, Runnable doneHandle) { this.url = url; this.doneHandle = doneHandle; } @Override protected byte[] doInBackground() throws Exception { var ret = img(url); return ret; } @Override protected void done() { try { imgCache.put(url, get()); } catch (Exception e) { System.err.println("加载图片出错"); } if (doneHandle != null) { doneHandle.run(); } } byte[] img(String url) throws IOException, InterruptedException, URISyntaxException { HttpRequest req = HttpRequest.newBuilder(new URI(url)) .GET().build(); var resp = HttpClient.newHttpClient() .send(req, HttpResponse.BodyHandlers.ofByteArray()); var ret = resp.body(); return ret; } }}