Java – How to resize JLabel ImageIcon

imagejavaswing

I'm making a Java Swing application that has the following layout (MigLayout):

[icon][icon][icon][....]
where icon = jlabel and the user can add more icons

When the user adds or removes icons, the others should shrink or grow.

My question is really straightforward: I have a JLabel which contains an ImageIcon; how can I resize this icon?

Best Answer

Try this :

ImageIcon imageIcon = new ImageIcon("./img/imageName.png"); // load the image to a imageIcon
Image image = imageIcon.getImage(); // transform it 
Image newimg = image.getScaledInstance(120, 120,  java.awt.Image.SCALE_SMOOTH); // scale it the smooth way  
imageIcon = new ImageIcon(newimg);  // transform it back

(found it here)