Java JLabel Size makeSameSize(int alignment, JLabel... labels)

Here you can find the source of makeSameSize(int alignment, JLabel... labels)

Description

make Same Size

License

LGPL

Declaration

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

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  ww  w  . j  a  v  a  2s. com
        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. adjustLabelSizes(List labels)
  2. getLabelPreferredSize(JLabel label)
  3. getPreferredLabelSize(JLabel c, int widthHint)
  4. getPreferredSize(JLabel label)
  5. resizeLabel(JLabel label, Dimension newDim)