             // where childWidth and childHeight are NaN, setLayoutBoundsSize will use preferredSize
             // which is already constrained between min and max.
             if (!isNaN(childWidth))
+            {
+                if (isNaN(elementMaxWidth))
+                    elementMaxWidth = layoutElement.getMaxBoundsWidth();
                 childWidth = Math.max(layoutElement.getMinBoundsWidth(), Math.min(elementMaxWidth, childWidth));
+            }
             if (!isNaN(childHeight))
+            {
+                if (isNaN(elementMaxHeight))
+                    elementMaxHeight = layoutElement.getMaxBoundsHeight();
                 childHeight = Math.max(layoutElement.getMinBoundsHeight(), Math.min(elementMaxHeight, childHeight));
+            }
 
             // Set the size.
             layoutElement.setLayoutBoundsSize(childWidth, childHeight);

                    availableHeight -= bottom;    
                    
                childHeight = Math.round(availableHeight * Math.min(percentHeight * 0.01, 1));
                elementMaxHeight = Math.min(elementMaxHeight,
                    maxSizeToFitIn(unscaledHeight, vCenter, top, bottom, layoutElement.getLayoutBoundsY()));
            }
            else if (!isNaN(top) && !isNaN(bottom))

                     availableWidth -= right;

                childWidth = Math.round(availableWidth * Math.min(percentWidth * 0.01, 1));
                elementMaxWidth = Math.min(layoutElement.getMaxBoundsWidth(),
                    maxSizeToFitIn(unscaledWidth, hCenter, left, right, layoutElement.getLayoutBoundsX()));
            }
            else if (!isNaN(left) && !isNaN(right))
