public Dimension preferredLayoutSize ( Container target ) { synchronized ( target . getTreeLock ( ) ) { Dimension dim = new Dimension ( 0 , 0 ) ; for ( int i = 0 ; i < target . getComponentCount ( ) ; i ++ ) { Component m = target . getComponent ( i ) ; if ( m . isVisible ( ) ) { Dimension d = m . getPreferredSize ( ) ; // original // dim.height = Math.max(dim.height, d.height); //if (i > 0) { dim.width += hgap; } // dim.width += d.width; // new way Point p = m . getLocation ( ) ; dim . width = Math . max ( dim . width , p . x + d . width ) ; dim . height = Math . max ( dim . height , p . y + d . height ) ; } } Insets insets = target . getInsets ( ) ; dim . width += insets . left + insets . right + getHgap ( ) * 2 ; dim . height += insets . top + insets . bottom + getVgap ( ) * 2 ; return dim ; } }
deal with having components on more than one line