/** * Iterates all child components and calculates the space enough to keep all of them assuming that * the width is fixed. */ private Dimension calculateSize(int maxRowWidth) { FlowLayout layout = (FlowLayout) getLayout(); int height = 0; int currentRowWidth = 0; int currentRowHeight = 0; for (int i = 0, count = getComponentCount(); i < count; ++i) { Component comp = getComponent(i); Dimension bounds = comp.getPreferredSize(); if (!comp.isVisible()) { continue; } currentRowHeight = Math.max(currentRowHeight, bounds.height); if (currentRowWidth + layout.getHgap() + bounds.width <= maxRowWidth) { if (bounds.width != 0) { currentRowWidth += bounds.width + layout.getHgap(); } continue; } height += currentRowHeight + layout.getVgap(); currentRowWidth = bounds.width; currentRowHeight = bounds.height; } return new Dimension(maxRowWidth, height + currentRowHeight + 2 * layout.getVgap()); }