How to resize an Image depending on the size of JLabel when the user select rows in database

孤者浪人 提交于 2021-02-05 11:29:07

问题


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!

来源:https://stackoverflow.com/questions/60403831/how-to-resize-an-image-depending-on-the-size-of-jlabel-when-the-user-select-rows

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!