Set image as icon for jLabel in java

This article will describe how to set a image as icon for a jLabel with example source code.The image will appear as the background of the jLabel.First the image object is created from the image file.Next the scaled instance image object is created.Next the ImageIcon object is created.The SetIcon method is ised to set the icon for the jLabel.

// set the big logo
    Image imgBigLogo = Toolkit.getDefaultToolkit().getImage(getClass().getResource("security_lock.png"));
    Image bigscaledInstance = imgBigLogo.getScaledInstance(50,50,Image.SCALE_SMOOTH);
    ImageIcon iconbig =new ImageIcon(bigscaledInstance);
    jLabelBigLogo.setIcon(iconbig);

50 x 50 is the size of the scaled image and can be changed.



That is all.

If you liked this article please do leave a reply and share it with friends.

Thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.