Displaying an image in a JTable
I am trying to display an image in column # 25 of the JTable: - For this I wrote the code below: -
jTable1.getColumnModel().getColumn(25).setCellRenderer(jTable1.getDefaultRenderer(ImageIcon.class));
String query = "SELECT name,type,supplier, department, manufacturer, model, serial_no, description, location, broker, dop, installation_date, expiring_date, retiring_date,warranty_info, icost, rcost, aarea, voltage, wattage, amperage, eline, capacity, plink, notes, adding_date, modification_date from assets where company_code='" + ims.MainWindow.cc + "' and adding_date between '" + new java.sql.Date(df.parse(df.format(jXDatePicker1.getDate())).getTime()) + "' and '" + new java.sql.Date(df.parse(df.format(jXDatePicker2.getDate())).getTime()) + "'";
System.out.println(query);
ResultSet rs = st.executeQuery(query);
int xx = 1;
byte[] imagedata=null;
ImageIcon format=null;
java.awt.Image originalImage=null;
java.awt.Image scaledImage=null;
javax.swing.ImageIcon newIconImage=null;
while (rs.next()) {
if(rs.getBytes(24)!=null){
imagedata=rs.getBytes(24);
format = new ImageIcon(imagedata);
originalImage = format.getImage();
scaledImage = originalImage.getScaledInstance(268, 200, java.awt.Image.SCALE_SMOOTH);
newIconImage = new javax.swing.ImageIcon(scaledImage);
}
mod.addRow(new Object[]{xx, rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5), rs.getString(6), rs.getString(7), rs.getString(8), rs.getString(9), rs.getString(10), rs.getString(11), rs.getString(12), rs.getString(13), rs.getString(14), rs.getString(15), rs.getString(16), rs.getString(17), rs.getString(18), rs.getString(19), rs.getString(20), rs.getString(21), rs.getString(22), rs.getString(23), newIconImage, rs.getString(25), rs.getString(26), rs.getString(27)});
xx++;
}
When I run a frame containing the code below, instead of showing the icon in the Image column, the image column displays " javax.swing.ImageIcon@119537a ". Pls help
source to share
When I run the frame containing the code below, instead of showing the Icon in the " javax.swing.ImageIcon@119537a " column is in the shown in the image column.
-
I think I need to set the correct ColumnClass for Icon / ImageIcons (in this case and only)
-
JTable knows the data type of Icon / ImageIcons in ColumnClass column , the same as
String
,Number
,Boolean
and ei
source to share
You can try changing the column number where you install the image icon display tool. 1. Try 24V
jTable1.getColumnModel().getColumn(25).setCellRenderer(jTable1.getDefaultRenderer(ImageIcon.class));
I suspect the rendering has not changed for the column where you added the image icon.
source to share
Maybe you should take a look at rendering a BufferedImage in a JTable cell , which is a similar question.
source to share