Jelle Keppels
Jelle Keppels

Reputation: 11

Resizing the Icon of a button

My JButtons are all resizable (when I resize the JFrame the JButtons adjust to the right size), however I encountered the problem that the Icons I have set for these JButtons do not resize. They stay the same height and width which they had when I initialised them. Is there a way to make the Icon of a JButton to scale with the size of the JButton?

Thanks in advance.

Upvotes: 0

Views: 2609

Answers (2)

trashgod
trashgod

Reputation: 205885

Implementations of the Icon interface represent "A small fixed size* picture, typically used to decorate components." The component holding the icon usually adjusts its preferred size according to the icon's dimensions and relative positioning. Buttons that resize with the container do so if the container's layout ignores the component's preferred size, e.g. GridLayout. Instead, consider a JComponent that renders it's content in a way that scales to its current size. Example include JDigit and LayoutTest.

* emphasis added

Upvotes: 1

Erik Pragt
Erik Pragt

Reputation: 14677

I guess the following will work, found with a simple google search...

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

See here: Auto-resizing JButton Icon and resizing a ImageIcon in a JButton

Upvotes: 1

Related Questions