0

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!

Christopher Schneider
  • 3,745
  • 2
  • 24
  • 38
zultz
  • 224
  • 2
  • 9
  • 2
    *However when i click other picture on JTable(database), it somehow gets back to its normal size* - I'm not sure what you are asking. The code you posted has nothing to do with a JTable. Post a proper [mre] demonstrating the problem. – camickr Feb 26 '20 at 01:58
  • One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). E.G. [This answer](https://stackoverflow.com/a/10862262/418556) hot links to an image embedded in [this question](https://stackoverflow.com/q/10861852/418556). – Andrew Thompson Feb 26 '20 at 02:59

0 Answers0