Render image size in table cell

1

I created a table in java that shows my images stored in blob in the database. However, the height of the table is not automatically resized by having to put a value manually, for example:

Tabela.setRowHeight(60);

I would like to get the image size of the database stored in an ImageIcon variable so that all cells with images are automatically resized.

modelo.addRow(new Object[]{numero, icon});

My table is thus knowing that the default for height is 60:

BeinganotheroftheoptionsrenderthesizeoftheimagestoredintheImageIconvariablebeforeplacingitinthetable,wouldthisbeagoodidea?Ideclaremyimagelikethis:

Blobblob=produto.getBlob("imagem");
ImageIcon icon = null;
try (InputStream is = blob.getBinaryStream()) {
      BufferedImage img = ImageIO.read(is);
      icon = new ImageIcon(img);
}

My table is created like this:

private DefaultTableModel modelo = new DefaultTableModel(new String[]{"Name", "Image"}, 0) {
            @Override
            public Class<?> getColumnClass(int columnIndex) {
                return columnIndex == 1 ? Icon.class : super.getColumnClass(columnIndex);
            }
        };

To insert the image into the table I use a button with the following code to select it:

JFileChooser chooser = new JFileChooser();
    chooser.showOpenDialog(null);
    f = chooser.getSelectedFile();
    d = chooser.getCurrentDirectory();
    n = chooser.getName(f);
    if(n!=null){
        jLabel14.setText(n);
    }else{
        jLabel14.setText("Nenhuma imagem selecionada.");
    }

After selecting the file click on a button to run a query that inserts it into the database:

FileInputStream fis=new FileInputStream(f);   
PreparedStatement ps=con.prepareStatement("insert into produtos (Nome,ID_Tipo_produto,Descricao,Preco,Data,Codigo,Referencia,Observacoes,Stock,Imagem) values ('" + txt_nome1.getText() + "','" + dados + "','" + txt_descricao.getText() + "','" + txt_preço1.getText() + "','" + txt_data.getText() + "','" + txt_codigo.getText() + "','" + txt_referencia.getText() + "','" + Observações.getText() + "','" + txt_stock.getText() + "',?)"); 
ps.setBinaryStream(1,fis,(int)f.length());
ps.executeUpdate();
ps.close();
fis.close();
con.close();
    
asked by anonymous 15.05.2017 / 01:09

1 answer

1

You can rewrite the renderer of your table, so that every time it identifies the data type ImageIcon in some cell, it sets the height of that row to the same of the image:

table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
            boolean hasFocus, int row, int column) {

        if (value instanceof ImageIcon) {
            ImageIcon icon = (ImageIcon) value;
            table.setRowHeight(row, icon.getIconHeight());
        }
        return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
    }
});

In this case, I used the setRowHeight() passing as a parameter which line will be changed and the height that should be, relative to the image size of that particular cell.

Note: Before implementing the above suggestion, I recommend that you change your model to the below:

private DefaultTableModel modelo = new DefaultTableModel(new String[]{"Name", "Image"}, 0) {
    @Override
    public Class<?> getColumnClass(int columnIndex) {
        return columnIndex == 1 ? ImageIcon.class : super.getColumnClass(columnIndex);
    }
};
    
16.05.2017 / 17:28