Here you can find the source of preferredSize(JComponent component)
public static Dimension preferredSize(JComponent component)
//package com.java2s; import java.awt.Dimension; import java.util.List; import javax.swing.JComponent; public class Main { public static Dimension preferredSize(JComponent component) { Dimension size = component.getPreferredSize(); component.setSize(size);/*from www . j a va 2 s . c o m*/ return size; } public static Dimension preferredSize(JComponent component, int overrideWidth, int overrideHeight) { Dimension size = component.getPreferredSize(); if (overrideWidth != -1) { size.width = overrideWidth; } if (overrideHeight != -1) { size.height = overrideHeight; } component.setSize(size); return size; } public static void preferredSize(List<JComponent> components) { for (JComponent component : components) { Dimension size = component.getPreferredSize(); component.setSize(size); } } public static void setSize(Dimension size, JComponent[] components) { for (JComponent component : components) { component.setSize(size); } } public static void setSize(Dimension size, List<JComponent> components) { for (JComponent component : components) { component.setSize(size); } } }