在Java中如何在JTable中显示URL中的图像

在Java中如何在JTable中显示URL中的图像,java,jtable,imageurl,Java,Jtable,Imageurl,我知道这将是一个重复的问题,但我找不到我的案件的答案。我有一个图像的URL(像这样说https://i.imgur.com/VcV0SG.jpg)在MySQL数据库中。所以我需要在JTable中渲染这些图像。我该怎么做 代码 java.lang.reflect.Type listType=new-TypeToken(){}.getType(); List productsList=new Gson().fromJson(json,listType); 对于(产品专业:产品列表) { Defaul

我知道这将是一个重复的问题,但我找不到我的案件的答案。我有一个图像的
URL
(像这样说
https://i.imgur.com/VcV0SG.jpg
)在MySQL数据库中。所以我需要在
JTable
中渲染这些图像。我该怎么做

代码

java.lang.reflect.Type listType=new-TypeToken(){}.getType();
List productsList=new Gson().fromJson(json,listType);
对于(产品专业:产品列表)
{
DefaultTableModel=(DefaultTableModel)productsTable.getModel();
向量行=新向量();
添加(pro.getCode());
添加(pro.getName());
添加(pro.getPrice());
//返回字符串(图像的url,如`https://i.imgur.com/VcV0SG.jpg`)
add(pro.getPic());
model.addRow(row);
}

我知道您知道如何从MySQL中提取数据。如果您已经有了数据,那么剩下的部分非常简单。使用
ImageIcon
。以下是一个例子:

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.net.MalformedURLException;
import java.net.URL;

public class Main extends JPanel {

   public static void main(String[] args) {
    EventQueue.invokeLater(() -> {
        try {
            showGui();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    });
   }

  public Main() throws MalformedURLException {
    Icon icon1 = new ImageIcon(new URL(
            "https://www.cleverpetproducts.com/wp-content/uploads/2018/03/tardar.jpg"));
    Icon icon2 = new ImageIcon(new URL(
            "https://www.cleverpetproducts.com/wp-content/uploads/2018/03/tardar.jpg"));
    Icon icon3 = new ImageIcon(new URL(
            "https://www.cleverpetproducts.com/wp-content/uploads/2018/03/tardar.jpg"));

    String[] columnNames = {"Picture", "Text"};
    Object[][] data = {
                    {icon1, "Text 1"},
                    {icon2, "Text 2"},
                    {icon3, "Text 3"},
     };

    DefaultTableModel model = new DefaultTableModel(data, columnNames) {
        public Class getColumnClass(int column) {
            return getValueAt(0, column).getClass();
        }
    };
    JTable table = new JTable(model);
    table.setPreferredSize(new Dimension(500, 500));
    table.getColumn(columnNames[0]).setPreferredWidth(300);
    table.getColumn(columnNames[1]).setPreferredWidth(100);
    table.setRowHeight(0, 100);
    table.setRowHeight(1, 100);
    table.setRowHeight(2, 100);

    JScrollPane scrollPane = new JScrollPane(table);
    add(scrollPane);
 }

 private static void showGui() throws MalformedURLException {
    JFrame frame = new JFrame("Icon showcase");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(new Main());
    frame.setLocationByPlatform(true);
    frame.pack();
    frame.setVisible(true);
 }

}

我添加了我的代码。我试过你的代码,但不起作用。那么我如何用我的代码获得图像呢<代码>代码在顶部。你能帮助我吗?我很感激这似乎是https问题,请尝试使用http url图像
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.net.MalformedURLException;
import java.net.URL;

public class Main extends JPanel {

   public static void main(String[] args) {
    EventQueue.invokeLater(() -> {
        try {
            showGui();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    });
   }

  public Main() throws MalformedURLException {
    Icon icon1 = new ImageIcon(new URL(
            "https://www.cleverpetproducts.com/wp-content/uploads/2018/03/tardar.jpg"));
    Icon icon2 = new ImageIcon(new URL(
            "https://www.cleverpetproducts.com/wp-content/uploads/2018/03/tardar.jpg"));
    Icon icon3 = new ImageIcon(new URL(
            "https://www.cleverpetproducts.com/wp-content/uploads/2018/03/tardar.jpg"));

    String[] columnNames = {"Picture", "Text"};
    Object[][] data = {
                    {icon1, "Text 1"},
                    {icon2, "Text 2"},
                    {icon3, "Text 3"},
     };

    DefaultTableModel model = new DefaultTableModel(data, columnNames) {
        public Class getColumnClass(int column) {
            return getValueAt(0, column).getClass();
        }
    };
    JTable table = new JTable(model);
    table.setPreferredSize(new Dimension(500, 500));
    table.getColumn(columnNames[0]).setPreferredWidth(300);
    table.getColumn(columnNames[1]).setPreferredWidth(100);
    table.setRowHeight(0, 100);
    table.setRowHeight(1, 100);
    table.setRowHeight(2, 100);

    JScrollPane scrollPane = new JScrollPane(table);
    add(scrollPane);
 }

 private static void showGui() throws MalformedURLException {
    JFrame frame = new JFrame("Icon showcase");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(new Main());
    frame.setLocationByPlatform(true);
    frame.pack();
    frame.setVisible(true);
 }

}