0

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.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Jelle Keppels
  • 11
  • 1
  • 2

2 Answers2

1

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

Community
  • 1
  • 1
Erik Pragt
  • 13,513
  • 11
  • 58
  • 64
  • Am I missing something or is this not auto-resizing the Icon? I will still have to enter values in order to resize the particular imageIcon. While the JButton auto resizes when I resize the JFrame. Is this the only way to resize the Icon? – Jelle Keppels Mar 27 '13 at 13:52
1

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

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045