I have a system where user can input pictures on it and I already search online on how to resize an image according to the size of JLabel and it works. However when i click other picture on JTable(database), it somehow gets back to its normal size and not resizing to the JLabel anymore. How would I fix this?
This is the code for the Upload Button
private void btnUploadActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(null);
File f = chooser.getSelectedFile();
filename = f.getAbsolutePath();
ImageIcon imageIcon = new ImageIcon (new ImageIcon(filename).getImage().getScaledInstance(lblImage.getWidth(), lblImage.getHeight(), Image.SCALE_DEFAULT));
lblImage.setIcon(imageIcon);
try {
File image = new File(filename);
FileInputStream fix = new FileInputStream(image);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
for (int number; (number = fix.read(buf)) != -1;) {
bos.write(buf, 0, number);
}
bookImage = bos.toByteArray();
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
}
Should I declare another getScaledInstance on other Buttons/Events? How?
Thanks!