Dimension () { final Dimension preferredSize = super.getPreferredSize(); int panelsHeight = Stream.of(myPanels).filter(Component::isVisible).mapToInt(panel -> panel.getPreferredSize().height).sum(); int visibleRowsHeight = getFontMetrics(BOLD_FONT).getHeight() * myMaxVisibleRows; if (panelsHeight <= visibleRowsHeight) { return preferredSize; } else { return new Dimension(preferredSize.width + 20, visibleRowsHeight); } }