Render table cell image size

Asked

Viewed 365 times

1

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

Tabela.setRowHeight(60);

I would like to get the size of the database image saved in an Imageicon variable for all cells with images to be automatically resized.

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

My table is like this knowing that I predefined the height to be 60:

Imagem da tabela

Since another option is to render the size of the image saved in the Imageicon variable before placing it in the table, would this be a good idea? I declare my image so:

Blob blob = 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 in 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 I 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();
  • 1

    Nor need to store, while loading an image, has to recover its size at runtime. Add how you build your table (if you use tablemodels, renderers, etc) to see how best to solve.

  • @diegofm Feito has here my method of inserting the image in the database and as I build the table, I would like to continue to be able to insert the image in the database in fromato blob.

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 only of that line 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 method setRowHeight() passing as parameter which line will be changed and the height it should be, this being relative to the image size of that particular cell.

Note: Before implementing the above suggestion, I recommend 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);
    }
};

Browser other questions tagged

You are not signed in. Login or sign up in order to post.