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?
To resize desktop icons, right-click (or press and hold) the desktop, point to View, then select Large icons, Medium icons, or Small icons.
Resizing the icon is not straightforward. You need to use Java's graphics 2D to scale the image. The first parameter is a Image class which you can easily get from ImageIcon class. You can use ImageIcon class to load your image file and then simply call getter method to get the image.
You can set a fixed the size by setting the minimum, preferred and maximum size: setMinimumSize(width, height); setPreferredSize(width, height); setMaximumSize(width, height);
getScaledInstance(jPanel. getWidth(),jPanel. getHeight(),Image. SCALE_SMOOTH);
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With