我创建了一个简单的应用程序来显示数据库中的图像。我在 MySQL 数据库中有一个表,其中有一列类型为BLOB。当我从表中检索图像时,它只包含:“javax.swing.ImageIcon@2143ca6”。我的代码:String[] columntabelNames = {"Images"};DefaultTableModel modelas = new DefaultTableModel(columntabelNames, 0);Statement stmt = null;ResultSet rs;try { Connection conn = getConnection(); stmt = (Statement) conn.createStatement(); ResultSet rs1; rs1 = stmt.executeQuery("SELECT IMAGES_IMAGE FROM dc_images"); if (rs1.next()) { byte[] imgData = rs1.getBytes("IMAGES_IMAGE"); ImageIcon imagIcon = new ImageIcon(imgData); Image im = imagIcon.getImage(); Image myImage = im.getScaledInstance(50, 50, Image.SCALE_SMOOTH); ImageIcon newImageIcon = new ImageIcon(myImage); lblimage.setIcon(newImageIcon); Object data[] = {newImageIcon}; modelas.addRow(data); } tabelImage.setModel(modelas);} catch (Exception ex) { System.out.println(ex.getMessage());}
2 回答
人到中年有点甜
TA贡献1895条经验 获得超7个赞
当我从表中检索图像时,它只包含:“javax.swing.ImageIcon@2143ca6”。
JTable 的默认渲染器只是调用对象上的 toString() 方法,因此您会看到 ImageIcon 的 toString()。
您需要覆盖getColumnClass(...)
您的TableModel
(或JTable
) 方法以返回Icon.class
,然后该表将使用 Icon 渲染器。
添加回答
举报
0/150
提交
取消