0

I am making a system where the user can save a picture and database. It is already saving, but I want that saved photo to be displayed on a JLabel when I click it from the JTable. I don't know the code on how make it appear, and I want it to be automatically resized according to the size of my JLabel.

Here is the code for the Table Mouse Clicked Event:

private void tblTableMouseClicked(java.awt.event.MouseEvent evt) {                                      
        int row = tblTable.getSelectedRow();
        String selection = tblTable.getModel().getValueAt(row, 0).toString();
        String sql = "select * from LibrarySystemDatabase where No = " + selection;
        try {
            pst = conn.prepareStatement(sql);
            rs = pst.executeQuery();
            if (rs.next()) {
                txtNo.setText(rs.getString("No"));
                txtTitle.setText(rs.getString("Title"));
                txtAuthor.setText(rs.getString("Author"));
                txtGenre.setText(rs.getString("Genre"));
                txtLexile.setText(rs.getString("Lexile"));
                txtPoints.setText(rs.getString("Points"));


            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e);
        } finally {
            try {
                rs.close();
                pst.close();
            } catch (Exception e) {
            }
        }
    }                            

Here 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);
        }
//*/
    }  
    byte[] bookImage = null;
    String filename = null;
    private ImageIcon format = null;

Thanks!

Member2017
  • 431
  • 2
  • 8
  • 16
zultz
  • 224
  • 2
  • 9
  • Is this question just about displaying an image on a JLabel at different sizes? It seems like you have a lot of extra information. – matt Feb 26 '20 at 08:27
  • I want the image to be automatically resized and displayed according to JLabel size – zultz Feb 26 '20 at 08:34
  • So this doesn't have anything to do with a database? What about solutions found here? https://stackoverflow.com/questions/16343098/resize-a-picture-to-fit-a-jlabel – matt Feb 26 '20 at 08:36
  • The images are save into database,what i want to happen to retrieve the image from database and show it into a JLabel – zultz Feb 26 '20 at 08:54
  • Right now you can retrieve and show the images? The only issue you're having is the scaling of the JLabel? Ie if I have an example of a jlabel that scales the displayed image to display as much as possible then you could put the reset together? – matt Feb 26 '20 at 08:55
  • I have a problem on showing and scaling the image. – zultz Feb 26 '20 at 09:19
  • 1
    Quit reposting questions. This is your 3rd question on this topic. Your question is NOT clear and you were asked in your last question: https://stackoverflow.com/q/60403831/131872 to post a [mre]. You haven't. Your question is about showing an image in a label. So all you need to demonstrate the problem is a JFrame with a JLabel (with a preferred size) and a JButton. When you click the button you read the image and scale it to the size of the label. The entire code will 20-30 lines. Then once you understand the basic concept of scaling an image to fit your label you fix your real code. – camickr Feb 26 '20 at 15:24
  • Also, you question looks similar to this question: https://stackoverflow.com/questions/60406636/how-to-put-getscaledinstance (a friend of yours?). That posting contains a solution where you don't even need to create a scaled instance of the image. It will dynamically resize. – camickr Feb 26 '20 at 15:29

1 Answers1

0

As it isn't clear when you'll know the size of your JLabel, I created a listener that can resize the JLabels icon when necessary.

static public JLabel getScalingJLabel(BufferedImage img){
    JLabel z1 = new JLabel( new ImageIcon(img) );
    z1.addComponentListener( new ComponentAdapter(){
        @Override
        public void componentResized(ComponentEvent e){
            JLabel label = (JLabel)e.getComponent();

            int w = img.getWidth();
            int h = img.getHeight();
            int cw = label.getWidth();
            int ch = label.getHeight();

            //constrains the sizes.
            int mw = cw>w ? w : cw;
            int mh = ch>h ? h : ch;

            //for iso-tropic scaling.
            if( mw != w || mh != h ){
                int scaled_height = h*mw/w;
                int scaled_width = w*mh/h;

                if (scaled_height>mh){
                    mw = scaled_width;
                } else{
                    mh = scaled_height;
                }
            }

            Icon i = label.getIcon();

            if( i.getIconWidth() == mw && i.getIconHeight() == mh ){
                //no change.
            } else{
                label.setIcon( new ImageIcon( img.getScaledInstance( mw, mh, Image.SCALE_FAST ) ) );
            }
        }
    } );
    return z1;
}

It looks like what you're doing, but I scale the icon when the label gets resized so you don't have to know the final dimensions, but swing will take care of it.

matt
  • 10,892
  • 3
  • 22
  • 34