Dimension (Container parent) { Insets i = parent.getInsets(); Dimension size = new Dimension(); Dimension nextButtonSize = myNextLabel.getPreferredSize(); FontMetrics fm = myTextPanel.getFontMetrics(myTextPanel.getFont()); for (Item item : myTexts) { int width = SwingUtilities.computeStringWidth(fm, item.toString()); if (item.icon != null) { width += myTextPanel.getIconTextGap() + item.icon.getIconWidth(); } width += nextButtonSize.width + i.left + i.right; int height = Math.max(fm.getHeight(), item.icon != null ? item.icon.getIconHeight() : 0) + i.top + i.bottom; size.width = Math.max(size.width, width); size.height = Math.max(size.height, Math.max(height, nextButtonSize.height)); } return size; }