java jtable 图片_如何在JAVA中从JTable中的URL显示图像

我了解您知道如何从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);

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值