如何在JTable中显示一张图片?
How to display a picture in a JTable?
我在尝试在 JTable
的单元格内显示图像时遇到一个小问题,它采用文本格式并显示图像本身。
icon.toString()
" returns:
我的代码:
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());
}
}
有人知道如何更正代码吗?
您需要覆盖 TableModel 中的 getColumnClass(int col) 方法,以便它 returns ImageIcon.class 为您想要显示 ImageIcon 的列,并将 ImageIcon 直接分配给 fila[ 2]:
public void loading() {
try {
String[]title = {"First Name","Last Name","Picture"};
String sql="select * from users";
model = new DefaultTableModel(null,title){
@Override
public Class<?> getColumnClass(int column) {
if (column==2) return ImageIcon.class;
return Object.class;
}
}
st = conn.createStatement();
ResultSet rs = st.executeQuery(sql);
Object[]fila = new Object[4];
while(rs.next()){
fila[0] = rs.getString("fna");
fila[1] = rs.getString("lna");
fila[2] = new ImageIcon(rs.getBytes("pic"));
model.addRow(fila);
}
tbl.setModel(model);
}
catch (SQLException e) {
JOptionPane.showMessageDialog(null, e.getMessage());
}
}
我在尝试在 JTable
的单元格内显示图像时遇到一个小问题,它采用文本格式并显示图像本身。
icon.toString()
" returns:
我的代码:
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());
}
}
有人知道如何更正代码吗?
您需要覆盖 TableModel 中的 getColumnClass(int col) 方法,以便它 returns ImageIcon.class 为您想要显示 ImageIcon 的列,并将 ImageIcon 直接分配给 fila[ 2]:
public void loading() {
try {
String[]title = {"First Name","Last Name","Picture"};
String sql="select * from users";
model = new DefaultTableModel(null,title){
@Override
public Class<?> getColumnClass(int column) {
if (column==2) return ImageIcon.class;
return Object.class;
}
}
st = conn.createStatement();
ResultSet rs = st.executeQuery(sql);
Object[]fila = new Object[4];
while(rs.next()){
fila[0] = rs.getString("fna");
fila[1] = rs.getString("lna");
fila[2] = new ImageIcon(rs.getBytes("pic"));
model.addRow(fila);
}
tbl.setModel(model);
}
catch (SQLException e) {
JOptionPane.showMessageDialog(null, e.getMessage());
}
}