0

I have created a simple application to display images from a database. I have a table in a MySQL database with a column of type BLOB.

When I retrieve an image from the table it just contains: "javax.swing.ImageIcon@2143ca6".

My code:

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());
}
DaveyDaveDave
  • 9,821
  • 11
  • 64
  • 77
  • See: https://stackoverflow.com/questions/5614875/how-to-set-icon-in-a-column-of-jtable/5615516#5615516 – camickr Aug 29 '18 at 14:08

2 Answers2

0

Try this

Image im = ImageIO.read((ImageInputStream) new DefaultStreamedContent(new ByteArrayInputStream(imgData)));
  • I'm trying you code but not working, my problem is when I add a ImageIcon "newImageIcon" to Array "data []" , and add Array "data []" to Jtabel "tabelimage ", because JLabel "lblimage" display the image retrieve database. thank you – mohammad saadeh Aug 29 '18 at 14:01
0

When I retrieve an image from the table it just contains: "javax.swing.ImageIcon@2143ca6".

The default renderer for the JTable simply invokes the toString() method on the object so you are seeing the toString() for the ImageIcon.

You need to override the getColumnClass(...) method of your TableModel (or JTable) to return Icon.class, then the table will use an Icon renderer.

Read the section from the Swing tutorial on Using Renderers/Editor for more information.

camickr
  • 321,443
  • 19
  • 166
  • 288