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