Here you can find the source of makeSamePreferredWidth(JComponent... comps)
Parameter | Description |
---|---|
comps | List of components to align sizes |
public static void makeSamePreferredWidth(JComponent... comps)
//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)); } } }