ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

如何在JAVA中从JTable中的URL显示图像

2019-10-25 08:03:13  阅读:217  来源: 互联网

标签:jtable imageurl java


我知道这将是一个重复的问题,但是我找不到我的案子的答案.我在MySQL数据库中有一个图像的URL(例如https://i.imgur.com/VcV0SG.jpg).因此,我需要在JTable中渲染这些图像.我怎样才能做到这一点?有谁能够帮助我?提前致谢.

Code

            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);
 }

}

标签:jtable,imageurl,java
来源: https://codeday.me/bug/20191025/1927005.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有