Java JButton Size setJButtonSizesTheSame(final JButton[] btns)

Here you can find the source of setJButtonSizesTheSame(final JButton[] btns)

Description

set J Button Sizes The Same

License

LGPL

Declaration

public static void setJButtonSizesTheSame(final JButton[] btns) 

Method Source Code


//package com.java2s;
import java.awt.Component;
import java.awt.Dimension;

import java.awt.FontMetrics;

import java.awt.Insets;

import java.awt.geom.Rectangle2D;

import javax.swing.JButton;
import javax.swing.JComboBox;

import javax.swing.JTextField;

import javax.swing.UIManager;

import javax.swing.plaf.metal.MetalLookAndFeel;

public class Main {
    static final String LAF_METAL = MetalLookAndFeel.class.getName();

    public static void setJButtonSizesTheSame(final JButton[] btns) {
        if (btns == null) {
            throw new IllegalArgumentException();
        }/*  w w w  .  jav a 2 s .  com*/

        final Dimension maxSize = new Dimension(0, 0);
        for (int i = 0; i < btns.length; ++i) {
            final JButton btn = btns[i];
            final FontMetrics fm = btn.getFontMetrics(btn.getFont());
            final Rectangle2D bounds = fm.getStringBounds(btn.getText(), btn.getGraphics());
            final int boundsHeight = (int) bounds.getHeight();
            final int boundsWidth = (int) bounds.getWidth();
            maxSize.width = boundsWidth > maxSize.width ? boundsWidth : maxSize.width;
            maxSize.height = boundsHeight > maxSize.height ? boundsHeight : maxSize.height;
        }

        final Insets insets = btns[0].getInsets();
        maxSize.width += insets.left + insets.right;
        maxSize.height += insets.top + insets.bottom;

        for (int i = 0; i < btns.length; ++i) {
            final JButton btn = btns[i];
            btn.setPreferredSize(maxSize);
        }

        initComponentHeight(btns);
    }

    public static void initComponentHeight(final Component... components) {
        if (components == null) {
            return;
        }
        for (final Component component : components) {
            if ((component instanceof JComboBox) || (component instanceof JButton)) {
                component.setPreferredSize(new Dimension(component.getPreferredSize().width, 22));
            } else if (component instanceof JTextField) {
                final String lf = UIManager.getLookAndFeel().getClass().getName();
                int i = 22;
                if (lf.equals(LAF_METAL)) {
                    i = 23;
                }
                component.setPreferredSize(new Dimension(component.getPreferredSize().width, i));
            }
        }
    }
}

Related

  1. equalizeSize(JButton[] aButtons)
  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)
  6. setJButtonSizesTheSame(JButton[] btns)
  7. setSizes(JButton[] aButtons, Dimension aPreferred, Dimension aMax)
  8. SetStandardSizeForButton(JButton btn)