Search code examples
javaswingjlabel

resizing image to fit exactly Jlabel of 300 by 300 px


I am retrieving an image from database that is needed to be fitted inside the JLabel of size 300 by 300. In this code the image is not resized instead a part of it is displayed inside the JLabel:

ResultSet r;

r = s.executeQuery("select * from  employee where emp_name='"+user+"'");

boolean v=r.next();
if (v==true) {
    add(r.getString("designation"));//to call add function 


    InputStream is = r.getBinaryStream(3);
    BufferedImage bimg = ImageIO.read(is);


    bimg.getScaledInstance(300,300,Image.SCALE_SMOOTH);

    ImageIcon n=new ImageIcon();
    n.setImage(bimg);

    l[1].setIcon(n);
}   

Solution

  • getScaledInstance() does not modify the original image. Do

    Image bimg = ImageIO.read(is);
    bimg = bimg.getScaledInstance(300,300,Image.SCALE_SMOOTH);
    

    instead.