Here you can find the source of packPanel(JPanel p, int width)
public static void packPanel(JPanel p, int width)
//package com.java2s; //License from project: Open Source License import java.awt.Component; import java.awt.Dimension; import javax.swing.JPanel; public class Main { public static void packPanel(JPanel p, int width) { int minh = 0; for (Component c : p.getComponents()) { minh += c.getMinimumSize().getHeight(); }//from w w w. j a va 2s. c o m p.setMinimumSize(new Dimension(width, minh)); p.setMaximumSize(new Dimension(width, minh)); } }