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:
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();
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.
– user28595
@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.
– Fábio Gouveia