0

I have a slight problem when trying to display an image on inside a cell in JTable, this takes a text format and shows the image itself.

E.G. icon.toString()" returns:

enter image description here

My code:

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());
    }
}

Anyone knows how the code can be corrected?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
lolo
  • 55
  • 1
  • 6
  • 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). – Andrew Thompson Jul 25 '15 at 21:43
  • See also [How to Use Tables - Concepts: Editors and Renderers](https://docs.oracle.com/javase/tutorial/uiswing/components/table.html#editrender).. – Andrew Thompson Jul 25 '15 at 21:44
  • possible duplicate of [Can't add image using ImageIcon to jTable cell](http://stackoverflow.com/questions/25378231/cant-add-image-using-imageicon-to-jtable-cell) – Ricardo Meneghin Filho Jul 25 '15 at 23:26

1 Answers1

2

You need to override the getColumnClass(int col) method in your TableModel so that it returns ImageIcon.class for the columns you want to display an ImageIcon, and assign the ImageIcon directly to 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());
    }
}
  • I have implemented this in several ways and not get any results. when i use `fila[2] = new ImageIcon (image);` if not placed `toString` I get an error – lolo Jul 25 '15 at 22:31
  • 1
    Friend this option has really helped me not know how much I appreciate, and I have more than one week working on it! Thank You Ricardo! – lolo Jul 25 '15 at 23:28