Index: javax/swing/BoxLayout.java =================================================================== RCS file: /cvsroot/classpath/classpath/javax/swing/BoxLayout.java,v retrieving revision 1.7 retrieving revision 1.8 diff -u -r1.7 -r1.8 --- javax/swing/BoxLayout.java 3 Mar 2005 10:34:27 -0000 1.7 +++ javax/swing/BoxLayout.java 12 Apr 2005 19:48:43 -0000 1.8 @@ -243,52 +243,150 @@ - insets.right, size.height - insets.bottom - insets.top); Component[] children = parent.getComponents(); + boolean[] laidOut = new boolean[children.length]; + for (int index = 0; index < laidOut.length; index++) + laidOut[index] = false; if (isHorizontalIn(parent)) { - int x = insets.left; + // compute overall preferred width + int preferredWidthAll = 0; + for (int index = 0; index < children.length; index++) + { + preferredWidthAll += children[index].getPreferredSize().width; + } + double widthFactor = (double) innerSize.width / + (double) preferredWidthAll; + + // sort out components that are constrained by minimum or maximum size + int widthRemain = innerSize.width; for (int index = 0; index < children.length; index++) { Component comp = children[index]; Dimension sz = comp.getPreferredSize(); - int width = sz.width; - int height = sz.height; - int cy = insets.top; - if (height > innerSize.height) + Dimension minSize = comp.getMinimumSize(); + Dimension maxSize = comp.getMaximumSize(); + int width = (int) (sz.width * widthFactor); + int height = Math.min(innerSize.height, maxSize.height); + // check min size + if (width < minSize.width) { - height = innerSize.height; + width = minSize.width; + comp.setSize(width, height); + laidOut[index] = true; + preferredWidthAll -= sz.width; + widthRemain -= width; + continue; } - else + // check max size + if (width > maxSize.width) { - cy = (int) ((innerSize.height - height) - * comp.getAlignmentY()); + width = maxSize.width; + comp.setSize(width, height); + laidOut[index] = true; + preferredWidthAll -= sz.width; + widthRemain -= width; + continue; } - - comp.setSize(width, height); + + } + + // recompute widthFactor for remaining components + widthFactor = (double) widthRemain / (double) preferredWidthAll; + + int x = insets.left; + + // lay out remaining comonents + for (int index = 0; index < children.length; index++) + { + Component comp = children[index]; + int width = 0; + + if (!laidOut[index]) + { + Dimension sz = comp.getPreferredSize(); + Dimension maxSize = comp.getMaximumSize(); + width = (int) (sz.width * widthFactor); + int height = Math.min(innerSize.height, maxSize.height); + comp.setSize(width, height); + } + else + width = comp.getWidth(); + + int cy = (int) ((innerSize.height - comp.getHeight()) + * comp.getAlignmentY() + insets.top); comp.setLocation(x, cy); x = x + width; } } else { - int y = insets.top; + // compute overall preferred height + int preferredHeightAll = 0; + for (int index = 0; index < children.length; index++) + { + preferredHeightAll += children[index].getPreferredSize().height; + } + double heightFactor = (double) innerSize.height / + (double) preferredHeightAll; + + // sort out components that are constrained by minimum or maximum size + int heightRemain = innerSize.height; for (int index = 0; index < children.length; index++) { Component comp = children[index]; Dimension sz = comp.getPreferredSize(); - int width = sz.width; - int height = sz.height; - int cx = insets.left; - if (width > innerSize.width) + Dimension minSize = comp.getMinimumSize(); + Dimension maxSize = comp.getMaximumSize(); + int height = (int) (sz.height * heightFactor); + int width = Math.min(innerSize.width, maxSize.width); + // check min size + if (height < minSize.height) { - width = innerSize.width; + height = minSize.height; + comp.setSize(width, height); + laidOut[index] = true; + preferredHeightAll -= sz.height; + heightRemain -= height; + continue; } - else + // check max size + if (height > maxSize.height) { - cx = (int) ((innerSize.width - width) * comp.getAlignmentX()); + height = maxSize.height; + comp.setSize(width, height); + laidOut[index] = true; + preferredHeightAll -= sz.height; + heightRemain -= height; + continue; } - - comp.setSize(width, height); + + } + + // recompute heightFactor for remaining components + heightFactor = (double) heightRemain / (double) preferredHeightAll; + + int y = insets.top; + + // lay out remaining comonents + for (int index = 0; index < children.length; index++) + { + Component comp = children[index]; + int height = 0; + + if (!laidOut[index]) + { + Dimension sz = comp.getPreferredSize(); + Dimension maxSize = comp.getMaximumSize(); + height = (int) (sz.height * heightFactor); + int width = Math.min(innerSize.width, maxSize.width); + comp.setSize(width, height); + } + else + height = comp.getHeight(); + + int cx = (int) ((innerSize.width - comp.getWidth()) + * comp.getAlignmentX() + insets.left); comp.setLocation(cx, y); y = y + height; }