Dimension () { Dimension size = new Dimension(); for (int i = 0; i < getComponentCount(); i++) { final Component each = getComponent(i); final Dimension eachSize = each.getPreferredSize(); size.width += eachSize.width; size.height = Math.max(size.height, eachSize.height); } return size; }