private Dimension layoutSize(Container target, boolean preferred) {
		synchronized (target.getTreeLock()) {
		int targetWidth = target.getSize().width;
		if (targetWidth == 0) {
			targetWidth = Integer.MAX_VALUE;
		}
		int hgap = getHgap();
		int vgap = getVgap();
		Insets insets = target.getInsets();
		int maxWidth = 
			targetWidth - (insets.left + insets.right + hgap*2);
		Dimension dim = new Dimension(0, 0);
		int rowWidth = 0;
		int rowHeight = 0;
		for (Component m: target.getComponents()) {
			if (m.isVisible()) {
				Dimension d = preferred
					? m.getPreferredSize() 
					: m.getMinimumSize();
				if (rowWidth + d.width > maxWidth) {
					addRow(dim, rowWidth, rowHeight);
					rowWidth = 0;
					rowHeight = 0;
				}
				if (rowWidth > 0) {
					rowWidth += hgap;
				}
				rowWidth += d.width;
				rowHeight = Math.max(rowHeight, d.height);
			}
		}
		addRow(dim, rowWidth, rowHeight);
		dim.width += insets.left + insets.right + hgap * 2;
		dim.height += insets.top + insets.bottom + vgap * 2;
		return dim;
	}
	}