0

How does one resize an image that will be placed in a JLabel?

ImageIcon icon = ImageIcon("img.gif");

JLabel label = new JLabel(icon);
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
BAR
  • 15,909
  • 27
  • 97
  • 185
  • 1
    Are you attached to `JLabel`? If not, check out [this](http://stackoverflow.com/questions/14548808/scale-the-imageicon-automatically-to-label-size/14553003#14553003) – MadProgrammer May 22 '13 at 04:37
  • Im not attached to it but it has to be simpler than a few lines.. right?? – BAR May 22 '13 at 04:43
  • 1
    Are you looking for a fixed size image, or do you want the Icon to dynamically resize as space is available? If it's the latter the check out [Stretch Icon](http://tips4java.wordpress.com/2012/03/31/stretch-icon/). – camickr May 22 '13 at 04:44
  • Not really. You need to jump through a few hoops to get the size of the icon to the size you want, as described in the link I provided... – MadProgrammer May 22 '13 at 04:52
  • You should be resizing the image and then let the label size itself based on the image. You can use image.getScaledImage(...) for the different size images. http://www.java-forums.org/new-java/23992-how-resize-images-fit-jlabel.html . A JLabel or JButton computes its preferred size according to is icon and/or text. If you need an image size that depends on the size of the component it is displayed in, custom painting may be a better option. http://www.java-forums.org/awt-swing/35208-help-how-auto-resize-image-icon-fit-jlabel.html http://stackoverflow.com/questions/6714045/how-to-resize-jlabel- –  May 22 '13 at 04:44

0 Answers0