Auto-resizing JButton Icon

Cristol.GdM picture Cristol.GdM · Nov 22, 2011 · Viewed 19.5k times · Source

so I have this JButtons to which I add Icons. The Icons are too large initially, so I resize them beforehand, and it works fine. Except that when I resize the window, the JButtons change size, but not the Icons, which is problematic.

Is there a way to have an Icon just fill the JButton it is attached to? Bit of code to make it clearer:

public JewelClass(){

    setBackground (new Color (30,30,30)); 
    addActionListener(this);
    setLayout(new GridLayout());

    ImageIcon icon = new ImageIcon(src/carre.jpg);
    setIcon (resizeIcon(icon,60,60));

}

resizeIcon being a personal function, that takes an Icon, a width parameter and a height parameter, and return a resized Icon (obviously). I tried changing the Layout, but it doesn't change anything. I tried getting the width/height of the JButton, but since they don't exist yet when the Icon is added, it doesn't work.

Would you guys have any idea how to get through this? It doesn't have to be an icon, as long as my JButton is filled with the image I give it, it's awesome :)

Thanks!

Answer

mKorbel picture mKorbel · Nov 22, 2011

in Swing you can add any JComponent to the another JComponent, for Image is JLabel the best JComponent, then why not put the JLabel#setIcon() to the JButton

enter image description here enter image description here

import java.awt.*;
import javax.swing.*;

public class ResizeIconInButton extends JFrame {
    private static final long serialVersionUID = 1L;

    public ResizeIconInButton() {
        JButton myButton = new JButton();
        myButton.setLayout(new BorderLayout());
        myButton.add(new CustomComponents0());
        add(myButton, BorderLayout.CENTER);
        setPreferredSize(getPreferredSize());
        setTitle("Resize Icon In Button");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                ResizeIconInButton main = new ResizeIconInButton();

            }
        };
        javax.swing.SwingUtilities.invokeLater(r);
    }
}

class CustomComponents0 extends JLabel {

    private static final long serialVersionUID = 1L;

    @Override
    public Dimension getMinimumSize() {
        return new Dimension(200, 100);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(300, 200);
    }

    @Override
    public void paintComponent(Graphics g) {
        int margin = 10;
        Dimension dim = getSize();
        super.paintComponent(g);
        g.setColor(Color.red);
        g.fillRect(margin, margin, dim.width - margin * 2, dim.height - margin * 2);
    }
}

EDIT:

import java.awt.*;
import javax.swing.*;

public class ResizeIconInButton extends JFrame {

    private static final long serialVersionUID = 1L;
    private static final String IMAGE_PATH = "http://duke.kenai.com/misc/Bullfight.jpg";
    private JButton myButton = new JButton();
    private JLabel myLabel = new JLabel();

    public ResizeIconInButton() {
        Icon myIcon = new ImageIcon(IMAGE_PATH);
        myLabel.setIcon(myIcon);
        myButton.setLayout(new BorderLayout());
        myButton.add(myLabel);
        add(myButton, BorderLayout.CENTER);
        setPreferredSize(new Dimension(200, 100));
        setTitle("Resize Icon In Button");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }

    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                ResizeIconInButton main = new ResizeIconInButton();
            }
        });
    }
}