繁体   English   中英

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

[英]How to display image from a URL in a JTable in Java

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

代码

            java.lang.reflect.Type listType = new TypeToken<ArrayList<Products>>() {}.getType();
            List<Products> productsList = new Gson().fromJson(json, listType);

            for(Products pro : productsList)
            {
                DefaultTableModel model = (DefaultTableModel) productsTable.getModel();
                Vector<String> row = new Vector<String>();

                row.add(pro.getCode());
                row.add(pro.getName());
                row.add(pro.getPrice());
                //returns String (url of the image like `https://i.imgur.com/VcV0SG.jpg`)
                row.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);
 }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM