【发布时间】:2015-10-16 08:27:13
【问题描述】:
尝试在JTable 的单元格内显示图像时,我遇到了一个小问题,这采用文本格式并显示图像本身。
例如icon.toString()" 返回:
我的代码:
public void loading() {
try {
String[]title = {"First Name","Last Name","Picture"};
String sql="select * from users";
model = new DefaultTableModel(null,title);
st = conn.createStatement();
ResultSet rs = st.executeQuery(sql);
String[]fila = new String[4];
while(rs.next()){
fila[0] = rs.getString("fna");
fila[1] = rs.getString("lna");
byte[] imgBytes = rs.getBytes("pic");
Blob blob = new javax.sql.rowset.serial.SerialBlob(imgBytes);
BufferedImage image = null;
try (InputStream is = blob.getBinaryStream()) {
image = ImageIO.read(is);
ImageIcon icon = new ImageIcon(image);
fila[2] = icon.toString();
} catch (IOException exp) {
exp.printStackTrace();
}
model.addRow(fila);
}
tbl.setModel(model);
}
catch (SQLException e) {
JOptionPane.showMessageDialog(null, e.getMessage());
}
}
有人知道如何更正代码吗?
【问题讨论】:
-
1) 为了尽快获得更好的帮助,请发帖 minimal reproducible example 或 Short, Self Contained, Correct Example。 2) 例如,获取图像的一种方法是热链接到在this Q&A 中看到的图像。
标签: java image swing jtable blob