Dimension () { Component component = isPreferredSizeSet() ? null : getVisibleComponent(); if (component == null) return super.getPreferredSize(); // preferred size of a visible component plus border insets of this panel Dimension size = component.getPreferredSize(); JBInsets.addTo(size, getInsets()); // add border of this panel return size; }