Gets the maximum preferred size from a group of components. - Java Swing

Java examples for Swing:JComponent

Description

Gets the maximum preferred size from a group of components.

Demo Code


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

public class Main {
    /**//from ww w . j  av  a  2 s  . c  o  m
     * Gets the maximum preferred size from a group of components. The maximum
     * preferred size is estimated the following way: the maximum preferred
     * height and the maximum preferred width is determined (over all
     * components) an {@link Dimension} object is created with this height and
     * width.
     * 
     * @param components
     *            the components to get the maximum preferred width and maximum
     *            preferred height from.
     * @return the maximum preferred width and maximum preferred height as a
     *         dimension object.
     */
    public static Dimension getMaximumPreferredSize(Component... components) {
        int maxWidth = 0;
        int maxHeight = 0;

        for (Component component : components) {
            Dimension preferredSize = component.getPreferredSize();
            if (preferredSize.getWidth() > maxWidth) {
                maxWidth = (int) preferredSize.getWidth();
            }
            if (preferredSize.getHeight() > maxHeight) {
                maxHeight = (int) preferredSize.getHeight();
            }
        }
        return new Dimension(maxWidth, maxHeight);
    }
}

Related Tutorials