0

I am new to Java and I met a little problem. Can someone please tell me how I can set the Image of a JButton to be auto resizable/fitable/stretchable in a JButton when I resize the whole JFrame? The JButton is in a JPanel with a GridLayout.

It will be great if someone could write a brief explanation. I need this to complete a calculator I am working on.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
user1990198
  • 169
  • 1
  • 3
  • 13

1 Answers1

2
  • Add component listener to the button.
  • Resize the Image on Fly and update Image on the button.

Image img = icon.getImage();  
Image newimg = img.getScaledInstance(NEW_WIDTH, NEW_HEIGHT, java.awt.Image.SCALE_SMOOTH);  
icon.setImage(newimg);
TT.
  • 15,774
  • 6
  • 47
  • 88
user1983527
  • 304
  • 1
  • 7