Java JButton Size equalizeSize(JButton[] aButtons)

Here you can find the source of equalizeSize(JButton[] aButtons)

Description

Resizes the given buttons making them equal in size.

License

Open Source License

Parameter

Parameter Description
aButtons Array of buttons to be made equal size.

Exception

Parameter Description
NullPointerException If <code>aButtons</code> is <code>null</code>.

Declaration

public static void equalizeSize(JButton[] aButtons) 

Method Source Code

//package com.java2s;
/*//  w  w  w. ja v  a 2  s. co  m
 * Copyright (c) 2006, 2007, 2008, 2010, Max Planck Institute for Informatics, Saarbruecken, Germany.
 *
 * This file is part of NetworkAnalyzer.
 * 
 * NetworkAnalyzer is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 * 
 * NetworkAnalyzer is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License along with NetworkAnalyzer. If not, see
 * <http://www.gnu.org/licenses/>.
 */

import java.awt.Dimension;

import javax.swing.JButton;

public class Main {
    /**
     * Resizes the given buttons making them equal in size.
     * <p>
     * The sizes of the buttons are equalized only by enlarging their widths and heights (when necessary).
     * </p>
     * 
     * @param aButtons
     *            Array of buttons to be made equal size.
     * 
     * @throws NullPointerException
     *             If <code>aButtons</code> is <code>null</code>.
     */
    public static void equalizeSize(JButton[] aButtons) {
        final Dimension preferredSize = aButtons[0].getPreferredSize();
        final Dimension maximumSize = aButtons[0].getMaximumSize();
        for (int i = 1; i < aButtons.length; ++i) {
            ensureSize(preferredSize, aButtons[i].getPreferredSize());
            ensureSize(maximumSize, aButtons[i].getMaximumSize());
        }
        setSizes(aButtons, preferredSize, maximumSize);
    }

    /**
     * Resizes the given buttons making them equal in size.
     * <p>
     * This a convenience method only. Calling this method is equivalent to calling:<br/> <code>
     * equalizeSize(new JButton[] { aButton1, aButton2 });
     * </code>
     * </p>
     * 
     * @param aButton1
     *            First of the buttons to be made equal in size.
     * @param aButton2
     *            Second of the buttons to be made equal in size.
     * @see #equalizeSize(JButton[])
     */
    public static void equalizeSize(JButton aButton1, JButton aButton2) {
        equalizeSize(new JButton[] { aButton1, aButton2 });
    }

    /**
     * Resizes the given buttons making them equal in size.
     * <p>
     * This a convenience method only. Calling this method is equivalent to calling:<br/> <code>
     * equalizeSize(new JButton[] { aButton1, aButton2, aButton3 });
     * </code>
     * </p>
     * 
     * @param aButton1
     *            First of the buttons to be made equal in size.
     * @param aButton2
     *            Second of the buttons to be made equal in size.
     * @param aButton3
     *            Third of the buttons to be made equal in size.
     * @see #equalizeSize(JButton[])
     */
    public static void equalizeSize(JButton aButton1, JButton aButton2, JButton aButton3) {
        equalizeSize(new JButton[] { aButton1, aButton2, aButton3 });
    }

    /**
     * Enlarges, if necessary, the given current size to cover the given other size.
     * <p>
     * If both the width and height of <code>aCurrentSize</code> are larger than the width and height of
     * <code>aSize</code>, respectively, calling this method has no effect.
     * </p>
     * 
     * @param aCurrentSize
     *            Size to be enlarged if necessary.
     * @param aSize
     *            Minimal required size of <code>aCurrentSize</code>.
     * 
     * @throws NullPointerException
     *             If any of the given parameters is <code>null</code>.
     */
    public static void ensureSize(Dimension aCurrentSize, Dimension aSize) {
        if (aCurrentSize.height < aSize.height) {
            aCurrentSize.height = aSize.height;
        }
        if (aCurrentSize.width < aSize.width) {
            aCurrentSize.width = aSize.width;
        }
    }

    /**
     * Sets the preferred and maximum sizes of the given buttons.
     * 
     * @param aButtons
     *            Buttons to be modified.
     * @param aPreferred
     *            Preferred size to be set to each of the buttons. If this is <code>null</code>, every
     *            button's preferred size is set to its default value.
     * @param aMax
     *            Maximum size to be set to each of the buttons. If this is <code>null</code>, every button's
     *            maximum size is set to its default value.
     * 
     * @throws NullPointerException
     *             If <code>aButtons</code> is <code>null</code>.
     */
    public static void setSizes(JButton[] aButtons, Dimension aPreferred, Dimension aMax) {
        for (final JButton button : aButtons) {
            button.setPreferredSize(aPreferred);
            button.setMaximumSize(aMax);
        }
    }
}

Related

  1. adjustSize(JButton button)
  2. makeButtonsSameSize(JButton[] buttons)
  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)