resizing a ImageIcon in a JButton

Anto picture Anto · May 18, 2010 · Viewed 76.5k times · Source

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?

Answer

tim_yates picture tim_yates · May 18, 2010
   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