Swing的JTable表格列中插入图片

1.实现效果

给swing的JTable实现如下效果,JTable的列表中插入图片

image-20210725233436734

2.实现步骤

这里准备了三张方式的图片:网络图片、工程类目录图片、本地磁盘图片

工程目录如下:

image-20210725233944024

关键步骤有两个:

  • 创建Icon对象
  • 重写DefaultTableModel类的getColumnClass方法

3.完整代码

package com.qy;

import java.awt.BorderLayout;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Enumeration;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;

/** 
* @Author 贺刘芳
* @Time 2021年7月25日 下午11:21:33 
* @Version 1.0
* <p>Description:</p>
*/
public class JTableImg extends JFrame {

	private JPanel contentPane;
	private JTable table;

	public static void main(String[] args) {
		JTableImg frame = new JTableImg();
		frame.setVisible(true);
	}

	public JTableImg() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置窗口关闭程序退出
		setSize(566, 429);//设置窗口大小
		setLocationRelativeTo(null);//设置窗口居中
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);//设置布局方式为绝对布局
		
		JPanel panel = new JPanel();
		panel.setBounds(10, 10, 530, 349);
		contentPane.add(panel);
		panel.setLayout(new BorderLayout(0, 0));
        //核心步骤1:创建Icon对象
		//相对于类路径读取图片
		Icon icon1 = new ImageIcon(this.getClass().getResource("/images/33.jpg"));
		//绝对路径读取图片
		Icon icon2 = new ImageIcon("D:/images/22.jpg");
		//网络路径读取图片
		Icon icon3 = null;
		try {
			icon3 = new ImageIcon(new URL("https://avatar.csdnimg.cn/3/F/F/3_toto1297488504_1548903051.jpg"));
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		table = new JTable();
		String[] columnNames = new String[] {"头像", "姓名"};
		//将图片Icon对象放入表格数据数组
		Object[][] data = new Object[][] {
			 {icon1, "Text 1"},
             {icon2, "Text 2"},
             {icon3, "Text 3"}
		};
		//将表格数据数组放入表格模型,并重写getColumnClass方法
		table.setModel(new DefaultTableModel(data,columnNames){
			@Override   //核心步骤2:重写getColumnClass方法
			public Class<?> getColumnClass(int columnIndex) {
				return getValueAt(0, columnIndex).getClass();
			}
		});
		//设置每一列的列宽
		//table.getColumn(columnNames[0]).setPreferredWidth(59);
		table.setRowHeight(0, 80);
	    table.setRowHeight(1, 80);
	    table.setRowHeight(2, 80);
	    table.setRowHeight(3, 80);
	    //fitTableColumns(table); //非必须,设置列宽随表格内容自动调整
		JScrollPane  jsp = new JScrollPane(table);
		jsp.setSize(530, 349);
		panel.add(jsp);
	}
	
	//设置列宽随表格内容自动调整   参考博客:https://blog.csdn.net/tototuzuoquan/article/details/8982618
	public void fitTableColumns(JTable myTable) { // 設置table的列寬隨內容調整
		JTableHeader header = myTable.getTableHeader();
		int rowCount = myTable.getRowCount();
		Enumeration columns = myTable.getColumnModel().getColumns();
		while (columns.hasMoreElements()) {
			TableColumn column = (TableColumn) columns.nextElement();
			int col = header.getColumnModel().getColumnIndex(column.getIdentifier());

			int width = (int) myTable.getTableHeader().getDefaultRenderer()
					.getTableCellRendererComponent(myTable,column.getIdentifier(), false, false, -1, col)
					.getPreferredSize().getWidth();
			for (int row = 0; row < rowCount; row++) {
				int preferedWidth = (int) myTable.getCellRenderer(row, col)
						.getTableCellRendererComponent(myTable,myTable.getValueAt(row, col), false, false,row, col)
						.getPreferredSize().getWidth();
				width = Math.max(width, preferedWidth);
			}
			header.setResizingColumn(column);
			column.setWidth(width + myTable.getIntercellSpacing().width);
		}

	}

}
posted @ 2021-07-26 00:07  青阳闲云  阅读(389)  评论(0编辑  收藏  举报