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
  • 291,719
  • 55
  • 540
  • 600
user1990198
  • 159
  • 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,428
  • 6
  • 44
  • 84
user1983527
  • 194
  • 7