38

I am creating a JButton which includes a specific ImageIcon. The main issue is that the original icon size is much bigger than the button size. As result when the button is displayed, only part of the icon can be seen. What is the method that "resize" an ImageIcon i n order to make it fit inside a JButton?

Anto
  • 4,265
  • 14
  • 63
  • 113

3 Answers3

71
   Image img = icon.getImage() ;  
   Image newimg = img.getScaledInstance( NEW_WIDTH, NEW_HEIGHT,  java.awt.Image.SCALE_SMOOTH ) ;  
   icon = new ImageIcon( newimg );

from http://www.coderanch.com/t/331731/GUI/java/Resize-ImageIcon

tim_yates
  • 167,322
  • 27
  • 342
  • 338
1

I would try to override the getIcon() method of JButton, and resize the super.getIcon(). (Or, redefining the lnf for that button.)

aioobe
  • 413,195
  • 112
  • 811
  • 826
0

I used this class

I included the code from "get the code" (at the bottom) as another class in my project. Be sure to change the package name if you want it to work.

Java Devil
  • 10,629
  • 7
  • 33
  • 48
Scott
  • 3
  • 2