I'm trying to resize a JButton
. I'm also trying to resize the image icon when the button is resized. I've used the ComponentListener
. Here is the snippet of code trying to resize the image icon when the button size is increased:
jButton1.addComponentListener(new ComponentListener() {
@Override
public void componentShown(ComponentEvent e) {
// ignore
}
@Override
public void componentResized(ComponentEvent e) {
Image newImg = img.getScaledInstance( jButton1.getWidth(), jButton1.getWidth(), java.awt.Image.SCALE_SMOOTH ) ;
ImageIcon icon = new ImageIcon(newImg);
jButton1.setIcon(icon);
}
@Override
public void componentMoved(ComponentEvent e) {
// ignore
}
@Override
public void componentHidden(ComponentEvent e) {
// ignore
}
});
But the problem is that only icon size is increased. Though the width is taken from the button itself. The button size stays fixed. If I do not change the icon, button size is increased accordingly.
Can anybody tell me what I'm missing here?