Question

Good day

I have a basic toolbar to which I added ImageIcon buttons. The images are however different in size. How would I go about in resizing the Icons so that they are all the same size.

super("ToolBar");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //creating the icons for the toolbar
    ImageIcon savePic = new ImageIcon("c:/Exercises/unitTwo/Chapter Three/Images/save.png");
    ImageIcon openFilePic = new ImageIcon("c:/Exercises/unitTwo/Chapter Three/Images/open.png");
    ImageIcon printPic = new ImageIcon("c:/Exercises/unitTwo/Chapter Three/Images/print.png");



    //creating buttons with initial text and icons. I.o.w. the buttons for the toolbar are created

    JButton save = new JButton("Save", savePic);
    JButton open = new JButton("Open", openFilePic);
    JButton print = new JButton("Print", printPic);


    JToolBar bar = new JToolBar();
    bar.add(save);
    bar.add(open);

    bar.add(new JToolBar.Separator());
    bar.add(print);

    JTextArea text = new JTextArea(10, 40);


    add(BorderLayout.NORTH, bar);
    add(BorderLayout.CENTER, text);

    pack();
    setVisible(true);
Was it helpful?

Solution

The flamingo component suite supports resizable icons, one of the support classes being ImageWrapperResizableIcon. You might try to have a look at the source to get an idea of how to implement automatically resizing icons without the need to manually do so.

Alternatively, just create a resized version of the image yourself and create the ImageIcon using that resized version.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top