Java JComponent Size makeSameSize(Component... components)

Here you can find the source of makeSameSize(Component... components)

Description

make Same Size

License

LGPL

Declaration

public static void makeSameSize(Component... components) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

import javax.swing.*;

import java.awt.*;

public class Main {
    public static void makeSameSize(Component... components) {
        makeSameHeight(components);/*from   w  ww  .j a  v  a  2 s . c o m*/
        makeSameWidth(components);
    }

    public static void makeSameSize(int alignment, JLabel... labels) {
        makeSameSize(labels);

        for (JLabel label : labels) {
            label.setHorizontalAlignment(alignment);
        }
    }

    public static void makeSameHeight(Component... components) {
        int maxHeight = 0;
        for (Component component : components) {
            if (component.getPreferredSize().height > maxHeight) {
                maxHeight = component.getPreferredSize().height;
            }
        }

        for (Component component : components) {
            final Dimension size = component.getPreferredSize();
            size.setSize(size.width, maxHeight);

            component.setMinimumSize(size);
            component.setPreferredSize(size);
            component.setMaximumSize(size);
        }
    }

    public static void makeSameWidth(Component... components) {
        int maxWidth = 0;
        for (Component component : components) {
            if (component.getPreferredSize().width > maxWidth) {
                maxWidth = component.getPreferredSize().width;
            }
        }

        for (Component component : components) {
            final Dimension size = component.getPreferredSize();
            size.setSize(maxWidth, size.height);

            component.setMinimumSize(size);
            component.setPreferredSize(size);
            component.setMaximumSize(size);
        }
    }
}

Related

  1. getVisibleSizeInViewport(Component c)
  2. getWindowSize(JComponent start)
  3. inset(Component c, int insetSizeHor, int insetSizeVert)
  4. isSmallSizeVariant(JComponent c)
  5. makeEqualSize(JComponent reference, JComponent... others)
  6. makeSameSize(JComponent... comps)
  7. makeSameSize(JComponent[] comps)
  8. minSize(T comp, int width)
  9. paint(final Component c, Dimension size)