You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在NetBeans中使用JTable显示图片

点击按钮后在JTable单元格中直接展示图片

点击按钮加载数据后,我需要让JTable的“image”列不再显示文本格式内容,而是直接展示图片本身。

当前显示效果(文本形式):
当前显示效果

期望显示效果(直接展示图片):
期望显示效果

以下是我目前实现数据加载的代码:

public void loading() {
    try {
        model = (DefaultTableModel) listofitemtable.getModel();
  
        String[] title = {"auction_no", "auction_name", "auction_type", "auction_date", "points_Per_Team", 
                          "minimum_Points", "points_Increase", "Player_per_Team", "image"};

        listofitemtable.getColumn("image").setCellRenderer(new meetable());     
        
        String sql = "select * from auction_create";
        model = new DefaultTableModel(null, title){                 
            @Override
            public Class<?> getColumnClass(int column) {
                // 指定第8列(索引从0开始)为ImageIcon类型
                if (column == 8) return ImageIcon.class;
                return Object.class;
            }
        };
        
        Statement st = conn.createStatement();
        rs = st.executeQuery(sql);
        Object[] fila = new Object[9];
        
        while(rs.next()){
            fila[0] = rs.getString("auction_no");
            fila[1] = rs.getString("auction_name");
            fila[2] = rs.getString("auction_type");
            fila[3] = rs.getString("auction_date");
            fila[4] = rs.getString("points_Per_Team");
            fila[5] = rs.getString("minimum_Points");
            fila[6] = rs.getString("points_Increase");
            fila[7] = rs.getString("Player_per_Team");          
            // 从数据库读取图片字节数据并转为ImageIcon
            fila[8] = new ImageIcon(rs.getBytes("image"));                        
            model.addRow(fila);           
        }         
        listofitemtable.setModel(model);
        // 设置行高以适配图片显示
        listofitemtable.setRowHeight(75);
        // 设置图片列的首选宽度
        listofitemtable.getColumnModel().getColumn(8).setPreferredWidth(50);
    }
    catch (SQLException e) {
        JOptionPane.showMessageDialog(null, e.getMessage());
    }
} 

内容的提问来源于stack exchange,提问作者christy v

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 03:17:20