public void layoutContainer(Container target) {
        synchronized (target.getTreeLock()) {
            Insets insets = target.getInsets();
            int maxwidth = target.getWidth() - (insets.left + insets.right + hgap*2);
            int maxheight = target.getHeight() - (insets.top + insets.bottom + vgap*2);
            int nmembers = target.getComponentCount();
            int x = insets.left + hgap, y = 0;
            int colw = 0, start = 0;

            boolean ltr = target.getComponentOrientation().isLeftToRight();

            for (int i = 0 ; i < nmembers ; i++) {
                Component m = target.getComponent(i);
                if (m.isVisible()) {
                    Dimension d = m.getPreferredSize();
                    if (maximizeOtherDimension) {
                        d.width = maxwidth;
                    }
                    m.setSize(d.width, d.height);

                    if ((y == 0) || ((y + d.height) <= maxheight)) {
                        if (y > 0) {
                            y += vgap;
                        }
                        y += d.height;
                        colw = Math.max(colw, d.width);
                    } else {
                        moveComponents(target, insets.left + hgap, y, maxheight - x, colw, start, i, ltr);
                        moveComponents(target, x, insets.top + vgap, colw, maxheight - y, start, i, ltr);
                        y = d.height;
                        x += hgap + colw;
                        colw = d.width;
                        start = i;
                    }
                }
            }
            moveComponents(target, x, insets.top + vgap, colw, maxheight - y, start, nmembers, ltr);
        }
    }