Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resize JLabel ImageIcon?

Tags:

java

image

swing

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?

like image 742
Marcos Roriz Junior Avatar asked Jul 15 '11 23:07

Marcos Roriz Junior


People also ask

How do I change ImageIcon size?

To resize desktop icons, right-click (or press and hold) the desktop, point to View, then select Large icons, Medium icons, or Small icons.

How do I change the size of an ImageIcon in Java?

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.

How do I change the size of a JLabel?

You can set a fixed the size by setting the minimum, preferred and maximum size: setMinimumSize(width, height); setPreferredSize(width, height); setMaximumSize(width, height);

How do I resize an image in a JPanel?

getScaledInstance(jPanel. getWidth(),jPanel. getHeight(),Image. SCALE_SMOOTH);


1 Answers

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)

like image 62
trolologuy Avatar answered Sep 19 '22 15:09

trolologuy