Dimension (Container parent) { int width = 0; int height = 0; int count = parent.getComponentCount(); int visibleCount = 0; for (int i = 0; i < count; i++) { Component component = parent.getComponent(i); if (!component.isVisible()) { continue; } Dimension size = component.getPreferredSize(); width += size.width; height = Math.max(height, size.height); visibleCount++; } if (visibleCount > 1) { width += (visibleCount - 1) * myOffset; } Dimension size = new Dimension(width, height); JBInsets.addTo(size, parent.getInsets()); return size; }