Java JComponent Width makeSamePreferredWidth(JComponent... comps)

Here you can find the source of makeSamePreferredWidth(JComponent... comps)

Description

Makes components use same width as the width of the widest component in the group

License

Open Source License

Parameter

Parameter Description
comps List of components to align sizes

Declaration

public static void makeSamePreferredWidth(JComponent... comps) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.awt.Dimension;
import java.util.Arrays;
import java.util.Comparator;
import javax.swing.JComponent;

public class Main {
    /**/*from  ww w.j av  a  2 s.  co m*/
     * Makes components use same width as the width of the widest component in the group
     * @param comps List of components to align sizes
     */
    public static void makeSamePreferredWidth(JComponent... comps) {
        if (comps.length == 0)
            return;
        Arrays.sort(comps, new Comparator<JComponent>() {
            @Override
            public int compare(JComponent o1, JComponent o2) {
                return o1.getPreferredSize().width - o2.getPreferredSize().width;
            }
        });
        int biggest = comps[comps.length - 1].getPreferredSize().width;
        for (JComponent comp : comps) {
            comp.setPreferredSize(new Dimension(biggest, comp.getPreferredSize().height));
            comp.setMaximumSize(new Dimension(biggest, comp.getMaximumSize().height));
            comp.setMinimumSize(new Dimension(biggest, comp.getMinimumSize().height));
        }
    }
}

Related

  1. getMinimumWidth(JComponent comp)
  2. getRectangle(JComponent comp)
  3. getRectangle(JComponent comp)
  4. initComponent(JComponent component, String title, int width, int height)
  5. makeEqualWidth(JComponent... components)
  6. normalizeWidth(JComponent... components)
  7. sameWidth(JComponent[] components, int width)
  8. setBoundsAndCenterHorizontally(JComponent component, int x, int y, int width, int height)
  9. setComponent3Width(JComponent c, int width)