Java JButton Size makeButtonsSameSize(JButton[] buttons)

Here you can find the source of makeButtonsSameSize(JButton[] buttons)

Description

make Buttons Same Size

License

Open Source License

Declaration

public static void makeButtonsSameSize(JButton[] buttons) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.awt.Dimension;

import javax.swing.JButton;

public class Main {
    public static void makeButtonsSameSize(JButton[] buttons) {

        if (buttons.length < 2) {
            return; // nothing to do
        }//from ww w . j  a  va2 s. c o  m

        Dimension size = buttons[0].getPreferredSize();
        int maxWidth = size.width;
        int maxHeight = size.height;
        int i;

        for (i = 1; i < buttons.length; i++) {
            size = buttons[i].getPreferredSize();
            if (size.width > maxWidth) {
                maxWidth = size.width;
            }
            if (size.height > maxHeight) {
                maxHeight = size.height;
            }
        }

        size = new Dimension(maxWidth, maxHeight);

        for (i = 0; i < buttons.length; i++) {
            buttons[i].setPreferredSize(size);
            buttons[i].setMinimumSize(size);
            buttons[i].setMaximumSize(size);
        }

    }
}

Related

  1. adjustSize(JButton button)
  2. equalizeSize(JButton[] aButtons)
  3. ScaleButtonIcon(javax.swing.JButton btn, int width, int height, int fontsize)
  4. setButtonSize(JButton button, Dimension size)
  5. setButtonSize(JButton button, int width, int height)
  6. setJButtonSizesTheSame(final JButton[] btns)