How can i change the size of the icon connected to the JButton

46 Views Asked by At

When you connect icon to JButton ( button.setIcon(icon); ) you get very small icon, How can I change the size of this icon?

My code: (In this code you need to drag 1 file to the frame and look how small the icon)

    JFrame frame = new JFrame();
    JPanel p = new JPanel();
    p.setTransferHandler(new TransferHandler(){
        @Override
        public boolean canImport(TransferHandler.TransferSupport support) {
            for (DataFlavor flavor : support.getDataFlavors()) {
                if (flavor.isFlavorJavaFileListType()) {
                    return true;
                }
            }
            return false;
        }
        @Override
        @SuppressWarnings("unchecked")
        public boolean importData(TransferHandler.TransferSupport support) {
            if (!this.canImport(support))
                return false;
            java.util.List<File> files;
            try {
                files = (List<File>) support.getTransferable()
                        .getTransferData(DataFlavor.javaFileListFlavor);
            } catch (UnsupportedFlavorException | IOException ex) {
                return false;
            }

            for (File file: files) {
                String name = file.getName();
                JPanel panel = new JPanel();

                Icon icon = FileSystemView.getFileSystemView().getSystemIcon(file);

                JButton button = new JButton(name);
                button.setIcon(icon);
                panel.add(button);
                p.add(panel);
                SwingUtilities.updateComponentTreeUI(frame);
            }
            return true;
        }
    });
    frame.add(p);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

In my expectation i want the option to change the size of the icon (I need the icon on the button)

I tried to get the img of the file (didn't work):

 Image img = ImageIO.read(file.getAbsolutePath());
1

There are 1 best solutions below

0
Grinding For Reputation On

Here's a simple program which allows you to control the size of the icon using your scroll wheel.

import java.awt.Image;
import java.io.File;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.filechooser.FileSystemView;

public class Main {

    File file = new File("path-to-your-file.png");
    Icon icon = FileSystemView.getFileSystemView().getSystemIcon(file);
    int size = 100;

    public Main() {
        JFrame jf = new JFrame();
        JLabel jl = new JLabel();

        jl.setIcon(icon);
        jf.addMouseWheelListener(e -> {
            
            size -= e.getWheelRotation();
            
            Image image = ((ImageIcon) icon).getImage().getScaledInstance(size, size, Image.SCALE_FAST);
            
            jl.setIcon(new ImageIcon(image));
            
        });
        
        jf.add(jl);
        
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setSize(500, 500);
        jf.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new Main());
    }

}

Here's how you can achieve this in your program

Step 1: Save the default icon in a new variable. We'll use icon as an example.
Step 2: When you want to resize the image, add an ImageIcon cast to your icon as shown in the program above. Make sure you have (ImageIcon) icon within brackets, else this will cast the final output to ImageIcon
Step 3: Use Image#getScaledInstance to scale the image you get from the ImageIcon.
Step 4: Finally change the JLabel icon to a new ImageIcon, with the image you had gotten from the previous step.

I hope this helps :)