public void acceptMinimumSize(Component c) { Dimension minimumSize = getMinimumSize(c); c.setMinimumSize( new Dimension( Math.max(minimumSize.width, c.getMinimumSize().width), Math.max(minimumSize.height, c.getMinimumSize().height))); }
public Dimension minimumLayoutSize(Container target) { Dimension dim = new Dimension(0, 0); int nmembers = target.getComponentCount(); for (int i = 0; i < nmembers; i++) { Component m = target.getComponent(i); if (m.isVisible()) { Dimension d = m.getMinimumSize(); if (fVerticalLayout) { dim.width = Math.max(dim.width, d.width); if (i > 0) { dim.height += fGap; } dim.height += d.height; } else { dim.height = Math.max(dim.height, d.height); if (i > 0) { dim.width += fGap; } dim.width += d.width; } } } Insets insets = target.getInsets(); dim.width += insets.left + insets.right; dim.width += 2 * fBorder.x; dim.height += insets.top + insets.bottom; dim.height += 2 * fBorder.y; return dim; }
private void setSizes(Container parent) { int nComps = parent.getComponentCount(); Dimension d = null; // Reset preferred/minimum width and height. preferredWidth = 0; preferredHeight = 0; minWidth = 0; minHeight = 0; for (int i = 0; i < nComps; i++) { Component c = parent.getComponent(i); if (c.isVisible()) { d = c.getPreferredSize(); if (i > 0) { preferredWidth += d.width / 2; preferredHeight += vgap; } else { preferredWidth = d.width; } preferredHeight += d.height; minWidth = Math.max(c.getMinimumSize().width, minWidth); minHeight = preferredHeight; } } }
/** * Determines the minimum size of the container argument using this grid layout. * * <p>The minimum width of a grid layout is the largest minimum width of all of the components in * the container times the number of columns, plus the horizontal padding times the number of * columns minus one, plus the left and right insets of the target container. * * <p>The minimum height of a grid layout is the largest minimum height of all of the components * in the container times the number of rows, plus the vertical padding times the number of rows * minus one, plus the top and bottom insets of the target container. * * @param parent the container in which to do the layout * @return the minimum dimensions needed to lay out the subcomponents of the specified container * @see java.awt.GridLayout#preferredLayoutSize * @see java.awt.Container#doLayout */ public Dimension minimumLayoutSize(Container parent) { synchronized (parent.getTreeLock()) { Insets insets = parent.getInsets(); int ncomponents = parent.getComponentCount(); int nrows = rows; int ncols = cols; if (nrows > 0) { ncols = (ncomponents + nrows - 1) / nrows; } else { nrows = (ncomponents + ncols - 1) / ncols; } int w = 0; int h = 0; for (int i = 0; i < ncomponents; i++) { Component comp = parent.getComponent(i); Dimension d = comp.getMinimumSize(); if (w < d.width) { w = d.width; } if (h < d.height) { h = d.height; } } return new Dimension( insets.left + insets.right + ncols * w + (ncols - 1) * hgap, insets.top + insets.bottom + nrows * h + (nrows - 1) * vgap); } }
@Override public Dimension minimumLayoutSize(Container parent) { Component toolbar = parent.getComponent(0); Dimension toolbarSize = toolbar.isVisible() ? toolbar.getMinimumSize() : new Dimension(); Dimension contentSize = parent.getComponent(1).getMinimumSize(); return new Dimension( Math.max(toolbarSize.width, contentSize.width), toolbarSize.height + contentSize.height); }
public void layoutContainer(Container parent) { Dimension ourSize = preferredLayoutSize(parent), // Does the following do something useful? // And shouldn't we cater to real instead of maximum sizes? // This way, if a VerticallyLaidout component had a particular size forced upon it, // wouldn't we neglect to scale down to using minimum sizes so long as its maximum // size was bigger than our preferred? all: [Davis] maxSize = parent.getMaximumSize(), size = parent.getSize(); Insets isets = parent.getInsets(); Dimension compSize; int n = parent.getComponentCount(); int y; boolean usingPreferredSize = true; if (ourSize.width > maxSize.width || ourSize.height > maxSize.height) { ourSize = minimumLayoutSize(parent); usingPreferredSize = false; } switch (verticalAlignment) { case TOP: y = isets.top; break; case BOTTOM: y = (size.height - ourSize.height); break; default: // assume MIDDLE y = (size.height - ourSize.height) / 2 + isets.top; break; } for (int i = 0; i < n; i++) { Component c = parent.getComponent(i); compSize = usingPreferredSize ? c.getPreferredSize() : c.getMinimumSize(); switch (horizontalAlignment) { case LEFT: c.setBounds(isets.left, y, compSize.width, compSize.height); break; case RIGHT: c.setBounds( ourSize.width - isets.right - compSize.width + isets.left, y, compSize.width, compSize.height); break; default: // assume CENTER c.setBounds( (ourSize.width - isets.left - isets.right - compSize.width + isets.left) / 2, y, compSize.width, compSize.height); } y += compSize.height + vgap; } }
/** * @param dim * @param cc */ private Dimension rozmer(final Dimension dim, final Component cc) { final Dimension prs = cc.getPreferredSize(); final Dimension mis = cc.getMinimumSize(); final Dimension mas = cc.getMaximumSize(); final int dx = rozmer(ratiox, dim.width, prs.width, mis.width, mas.width); final int dy = rozmer(ratioy, dim.height, prs.height, mis.height, mas.height); final Dimension v = new Dimension(dx, dy); return v; }
public Dimension preferredLayoutSize(Container parent) { Insets insets = parent.insets(); int ncomponents = parent.countComponents(); int nrows = getRows(); int ncols = getColumns(); int hgap = getHgap(); int vgap = getVgap(); if (nrows > 0) { ncols = (ncomponents + nrows - 1) / nrows; } else { nrows = (ncomponents + ncols - 1) / ncols; } int nComps = parent.getComponentCount(); int y = insets.top; for (int row = 0; row < nrows; row++) { int h = 0; for (int col = 0; col < ncols; col++) { if (row * ncols + col < nComps) { Component c = parent.getComponent(row * ncols + col); h = Math.max(h, c.getMinimumSize().height); } } y += h + vgap; } int x = insets.left; for (int col = 0; col < ncols; col++) { int w = 0; for (int row = 0; row < nrows; row++) { if (row * ncols + col < nComps) { Component c = parent.getComponent(row * ncols + col); w = Math.max(w, c.getMinimumSize().width); } } x += w + hgap; } return new Dimension(x, y); }
/** * Calculates the minimum size dimensions for the specified panel given the components in the * specified parent container. */ public Dimension minimumLayoutSize(Container container) { Component child = getChild(container); if (child == null) return new Dimension(0, 0); Dimension childMinSize = child.getMinimumSize(); Insets insets = container.getInsets(); int width = childMinSize.width + insets.left + insets.right; int height = childMinSize.height + insets.top + insets.bottom; int maxSize = Math.max(width, height); return new Dimension(maxSize, maxSize); }
/** Lays out the container in the specified panel. */ public void layoutContainer(Container target) { synchronized (target.getTreeLock()) { Insets insets = target.getInsets(); Dimension dim = target.getSize(); int fullHeight = dim.height; int bottom = dim.height - insets.bottom - 1; int top = 0 + insets.top; int left = insets.left; int maxPosition = dim.width - (insets.left + insets.right) - hgap; int right = dim.width - insets.right; maxPosition = right; int height = bottom - top; int size = actions.size(); int w, h; if ((grip != null) && grip.isVisible()) { Dimension d = grip.getPreferredSize(); grip.setBounds(left, top + vgap, d.width, bottom - top - 2 * vgap); left += d.width; left += hgap; } for (int i = 0; i < size; i++) { left += hgap; Component comp = (Component) actions.elementAt(i); Dimension d; Dimension minSize = comp.getMinimumSize(); if (i == size - 1) d = comp.getMaximumSize(); else d = comp.getPreferredSize(); w = d.width; h = Math.min(height, d.height); if ((left < maxPosition) && (left + w > maxPosition)) { if (maxPosition - left >= minSize.width) w = maxPosition - left; } comp.setBounds(left, (fullHeight - d.height) / 2, w, h); // if (i == size - 1) // d = comp.getMaximumSize(); // else // d = comp.getPreferredSize(); // if ((left + d.width) > right) // w = right - left; // else // w = d.width; // h = Math.min (height, d.height); // comp.setBounds (left, (fullHeight - d.height)/2, w, h); left += d.width; } } }
/** * Calculates the minimum dimension for the specified panel given the components in the specified * parent container. * * @param parent the component to be laid out * @see #preferredLayoutSize */ public Dimension minimumLayoutSize(Container parent) { int maxWidth = 0; int maxHeight = 0; for (java.util.Enumeration e = constraints.keys(); e.hasMoreElements(); ) { Component comp = (Component) e.nextElement(); AbsoluteConstraints ac = (AbsoluteConstraints) constraints.get(comp); Dimension size = comp.getMinimumSize(); int width = ac.getWidth(); if (width == -1) width = size.width; int height = ac.getHeight(); if (height == -1) height = size.height; if (ac.x + width > maxWidth) maxWidth = ac.x + width; if (ac.y + height > maxHeight) maxHeight = ac.y + height; } return new Dimension(maxWidth, maxHeight); }
public Dimension minimumLayoutSize(Container target) { Insets insets = target.getInsets(); Dimension dim = new Dimension(0, 0); int ncomponents = target.getComponentCount(); Component comp; Dimension d; for (int i = 0; i < ncomponents; i++) { comp = target.getComponent(i); if (comp.isVisible()) { d = comp.getMinimumSize(); dim.width += d.width; dim.height = Math.max(d.height, dim.height); if (i > 0) dim.width += gap; } } dim.width += insets.left + insets.right; dim.height += insets.top + insets.bottom; return dim; }
/** * Algorithm for calculating the largest minimum or preferred cell size. * * <p>Largest cell size is calculated by getting the applicable size of each component and keeping * the maximum value, dividing the component's width by the number of columns it is specified to * occupy and dividing the component's height by the number of rows it is specified to occupy. * * @param parent the container in which to do the layout. * @param isPreferred true for calculating preferred size, false for calculating minimum size. * @return the largest cell size required. */ protected Dimension getLargestCellSize(Container parent, boolean isPreferred) { int ncomponents = parent.getComponentCount(); Dimension maxCellSize = new Dimension(0, 0); for (int i = 0; i < ncomponents; i++) { Component c = parent.getComponent(i); Rectangle rect = (Rectangle) compTable.get(c); if (c != null && rect != null) { Dimension componentSize; if (isPreferred) { componentSize = c.getPreferredSize(); } else { componentSize = c.getMinimumSize(); } // Note: rect dimensions are already asserted to be > 0 when the // component is added with constraints maxCellSize.width = Math.max(maxCellSize.width, componentSize.width / rect.width); maxCellSize.height = Math.max(maxCellSize.height, componentSize.height / rect.height); } } return maxCellSize; }
public void layoutContainer(Container target) { Insets insets = target.getInsets(); int nmembers = target.getComponentCount(); int x = insets.left + fBorder.x; int y = insets.top + fBorder.y; for (int i = 0; i < nmembers; i++) { Component m = target.getComponent(i); if (m.isVisible()) { Dimension d = m.getMinimumSize(); m.setBounds(x, y, d.width, d.height); if (fVerticalLayout) { y += d.height; y += fGap; } else { x += d.width; x += fGap; } } } }
/** * A debugging utility that prints to stdout the component's minimum, preferred, and maximum * sizes. */ public static void printSizes(Component c) { System.out.println("minimumSize = " + c.getMinimumSize()); System.out.println("preferredSize = " + c.getPreferredSize()); System.out.println("maximumSize = " + c.getMaximumSize()); }
// @Override // public Component prepareEditor(TableCellEditor editor, int row, int column) { // Component prepareEditor = super.prepareEditor(editor, row, column); // customiseMinimumDimensions(prepareEditor.getMinimumSize(), row, column); // return prepareEditor; // } @Override public Component prepareRenderer(TableCellRenderer renderer, int row, int column) { Component prepareRenderer = super.prepareRenderer(renderer, row, column); customiseMinimumDimensions(prepareRenderer.getMinimumSize(), row, column); return prepareRenderer; }
/* * (non-Javadoc) * * @see java.awt.LayoutManager2#maximumLayoutSize(java.awt.Container) */ @Override public Dimension maximumLayoutSize(final Container c) { return podklad.getMinimumSize(); }
/** * Creates the appropriate object to represent each of the objects in <code>buttons</code> and * adds it to <code>container</code>. This differs from addMessageComponents in that it will * recurse on <code>buttons</code> and that if button is not a Component it will create an * instance of JButton. */ protected void addButtonComponents(Container container, Object[] buttons, int initialIndex) { if (buttons != null && buttons.length > 0) { boolean sizeButtonsToSame = getSizeButtonsToSameWidth(); boolean createdAll = true; int numButtons = buttons.length; JButton[] createdButtons = null; int maxWidth = 0; if (sizeButtonsToSame) { createdButtons = new JButton[numButtons]; } for (int counter = 0; counter < numButtons; counter++) { Object button = buttons[counter]; Component newComponent; if (button instanceof Component) { createdAll = false; newComponent = (Component) button; container.add(newComponent); hasCustomComponents = true; } else { JButton aButton; if (button instanceof ButtonFactory) { aButton = ((ButtonFactory) button).createButton(); } else if (button instanceof Icon) aButton = new JButton((Icon) button); else aButton = new JButton(button.toString()); aButton.setName("OptionPane.button"); aButton.setMultiClickThreshhold( DefaultLookup.getInt(optionPane, this, "OptionPane.buttonClickThreshhold", 0)); configureButton(aButton); container.add(aButton); ActionListener buttonListener = createButtonActionListener(counter); if (buttonListener != null) { aButton.addActionListener(buttonListener); } newComponent = aButton; } if (sizeButtonsToSame && createdAll && (newComponent instanceof JButton)) { createdButtons[counter] = (JButton) newComponent; maxWidth = Math.max(maxWidth, newComponent.getMinimumSize().width); } if (counter == initialIndex) { initialFocusComponent = newComponent; if (initialFocusComponent instanceof JButton) { JButton defaultB = (JButton) initialFocusComponent; defaultB.addHierarchyListener( new HierarchyListener() { public void hierarchyChanged(HierarchyEvent e) { if ((e.getChangeFlags() & HierarchyEvent.PARENT_CHANGED) != 0) { JButton defaultButton = (JButton) e.getComponent(); JRootPane root = SwingUtilities.getRootPane(defaultButton); if (root != null) { root.setDefaultButton(defaultButton); } } } }); } } } ((ButtonAreaLayout) container.getLayout()) .setSyncAllWidths((sizeButtonsToSame && createdAll)); /* Set the padding, windows seems to use 8 if <= 2 components, otherwise 4 is used. It may actually just be the size of the buttons is always the same, not sure. */ if (DefaultLookup.getBoolean(optionPane, this, "OptionPane.setButtonMargin", true) && sizeButtonsToSame && createdAll) { JButton aButton; int padSize; padSize = (numButtons <= 2 ? 8 : 4); for (int counter = 0; counter < numButtons; counter++) { aButton = createdButtons[counter]; aButton.setMargin(new Insets(2, padSize, 2, padSize)); } } } }
/** * This method is obsolete and supplied for backwards compatability only; new code should call * {@link #getLayoutInfo(Container, int) getLayoutInfo} instead. */ protected GridBagLayoutInfo GetLayoutInfo(Container parent, int sizeflag) { synchronized (parent.getTreeLock()) { GridBagLayoutInfo r = new GridBagLayoutInfo(); Component comp; GridBagConstraints constraints; Dimension d; Component components[] = parent.getComponents(); int compindex, i, j, k, px, py, pixels_diff, nextSize; int curX, curY, curWidth, curHeight, curRow, curCol; double weight_diff, weight, start, size; int xMax[], yMax[]; /* * Pass #1 * * Figure out the dimensions of the layout grid (use a value of 1 for * zero or negative widths and heights). */ r.width = r.height = 0; curRow = curCol = -1; xMax = new int[MAXGRIDSIZE]; yMax = new int[MAXGRIDSIZE]; for (compindex = 0; compindex < components.length; compindex++) { comp = components[compindex]; if (!comp.isVisible()) continue; constraints = lookupConstraints(comp); curX = constraints.gridx; curY = constraints.gridy; curWidth = constraints.gridwidth; if (curWidth <= 0) curWidth = 1; curHeight = constraints.gridheight; if (curHeight <= 0) curHeight = 1; /* If x or y is negative, then use relative positioning: */ if (curX < 0 && curY < 0) { if (curRow >= 0) curY = curRow; else if (curCol >= 0) curX = curCol; else curY = 0; } if (curX < 0) { px = 0; for (i = curY; i < (curY + curHeight); i++) px = Math.max(px, xMax[i]); curX = px - curX - 1; if (curX < 0) curX = 0; } else if (curY < 0) { py = 0; for (i = curX; i < (curX + curWidth); i++) py = Math.max(py, yMax[i]); curY = py - curY - 1; if (curY < 0) curY = 0; } /* Adjust the grid width and height */ for (px = curX + curWidth; r.width < px; r.width++) ; for (py = curY + curHeight; r.height < py; r.height++) ; /* Adjust the xMax and yMax arrays */ for (i = curX; i < (curX + curWidth); i++) { yMax[i] = py; } for (i = curY; i < (curY + curHeight); i++) { xMax[i] = px; } /* Cache the current slave's size. */ if (sizeflag == PREFERREDSIZE) d = comp.getPreferredSize(); else d = comp.getMinimumSize(); constraints.minWidth = d.width; constraints.minHeight = d.height; /* Zero width and height must mean that this is the last item (or * else something is wrong). */ if (constraints.gridheight == 0 && constraints.gridwidth == 0) curRow = curCol = -1; /* Zero width starts a new row */ if (constraints.gridheight == 0 && curRow < 0) curCol = curX + curWidth; /* Zero height starts a new column */ else if (constraints.gridwidth == 0 && curCol < 0) curRow = curY + curHeight; } /* * Apply minimum row/column dimensions */ if (columnWidths != null && r.width < columnWidths.length) r.width = columnWidths.length; if (rowHeights != null && r.height < rowHeights.length) r.height = rowHeights.length; /* * Pass #2 * * Negative values for gridX are filled in with the current x value. * Negative values for gridY are filled in with the current y value. * Negative or zero values for gridWidth and gridHeight end the current * row or column, respectively. */ curRow = curCol = -1; xMax = new int[MAXGRIDSIZE]; yMax = new int[MAXGRIDSIZE]; for (compindex = 0; compindex < components.length; compindex++) { comp = components[compindex]; if (!comp.isVisible()) continue; constraints = lookupConstraints(comp); curX = constraints.gridx; curY = constraints.gridy; curWidth = constraints.gridwidth; curHeight = constraints.gridheight; /* If x or y is negative, then use relative positioning: */ if (curX < 0 && curY < 0) { if (curRow >= 0) curY = curRow; else if (curCol >= 0) curX = curCol; else curY = 0; } if (curX < 0) { if (curHeight <= 0) { curHeight += r.height - curY; if (curHeight < 1) curHeight = 1; } px = 0; for (i = curY; i < (curY + curHeight); i++) px = Math.max(px, xMax[i]); curX = px - curX - 1; if (curX < 0) curX = 0; } else if (curY < 0) { if (curWidth <= 0) { curWidth += r.width - curX; if (curWidth < 1) curWidth = 1; } py = 0; for (i = curX; i < (curX + curWidth); i++) py = Math.max(py, yMax[i]); curY = py - curY - 1; if (curY < 0) curY = 0; } if (curWidth <= 0) { curWidth += r.width - curX; if (curWidth < 1) curWidth = 1; } if (curHeight <= 0) { curHeight += r.height - curY; if (curHeight < 1) curHeight = 1; } px = curX + curWidth; py = curY + curHeight; for (i = curX; i < (curX + curWidth); i++) { yMax[i] = py; } for (i = curY; i < (curY + curHeight); i++) { xMax[i] = px; } /* Make negative sizes start a new row/column */ if (constraints.gridheight == 0 && constraints.gridwidth == 0) curRow = curCol = -1; if (constraints.gridheight == 0 && curRow < 0) curCol = curX + curWidth; else if (constraints.gridwidth == 0 && curCol < 0) curRow = curY + curHeight; /* Assign the new values to the gridbag slave */ constraints.tempX = curX; constraints.tempY = curY; constraints.tempWidth = curWidth; constraints.tempHeight = curHeight; } /* * Apply minimum row/column dimensions and weights */ if (columnWidths != null) System.arraycopy(columnWidths, 0, r.minWidth, 0, columnWidths.length); if (rowHeights != null) System.arraycopy(rowHeights, 0, r.minHeight, 0, rowHeights.length); if (columnWeights != null) System.arraycopy(columnWeights, 0, r.weightX, 0, columnWeights.length); if (rowWeights != null) System.arraycopy(rowWeights, 0, r.weightY, 0, rowWeights.length); /* * Pass #3 * * Distribute the minimun widths and weights: */ nextSize = Integer.MAX_VALUE; for (i = 1; i != Integer.MAX_VALUE; i = nextSize, nextSize = Integer.MAX_VALUE) { for (compindex = 0; compindex < components.length; compindex++) { comp = components[compindex]; if (!comp.isVisible()) continue; constraints = lookupConstraints(comp); if (constraints.tempWidth == i) { px = constraints.tempX + constraints.tempWidth; /* right column */ /* * Figure out if we should use this slave\'s weight. If the weight * is less than the total weight spanned by the width of the cell, * then discard the weight. Otherwise split the difference * according to the existing weights. */ weight_diff = constraints.weightx; for (k = constraints.tempX; k < px; k++) weight_diff -= r.weightX[k]; if (weight_diff > 0.0) { weight = 0.0; for (k = constraints.tempX; k < px; k++) weight += r.weightX[k]; for (k = constraints.tempX; weight > 0.0 && k < px; k++) { double wt = r.weightX[k]; double dx = (wt * weight_diff) / weight; r.weightX[k] += dx; weight_diff -= dx; weight -= wt; } /* Assign the remainder to the rightmost cell */ r.weightX[px - 1] += weight_diff; } /* * Calculate the minWidth array values. * First, figure out how wide the current slave needs to be. * Then, see if it will fit within the current minWidth values. * If it will not fit, add the difference according to the * weightX array. */ pixels_diff = constraints.minWidth + constraints.ipadx + constraints.insets.left + constraints.insets.right; for (k = constraints.tempX; k < px; k++) pixels_diff -= r.minWidth[k]; if (pixels_diff > 0) { weight = 0.0; for (k = constraints.tempX; k < px; k++) weight += r.weightX[k]; for (k = constraints.tempX; weight > 0.0 && k < px; k++) { double wt = r.weightX[k]; int dx = (int) ((wt * ((double) pixels_diff)) / weight); r.minWidth[k] += dx; pixels_diff -= dx; weight -= wt; } /* Any leftovers go into the rightmost cell */ r.minWidth[px - 1] += pixels_diff; } } else if (constraints.tempWidth > i && constraints.tempWidth < nextSize) nextSize = constraints.tempWidth; if (constraints.tempHeight == i) { py = constraints.tempY + constraints.tempHeight; /* bottom row */ /* * Figure out if we should use this slave's weight. If the weight * is less than the total weight spanned by the height of the cell, * then discard the weight. Otherwise split it the difference * according to the existing weights. */ weight_diff = constraints.weighty; for (k = constraints.tempY; k < py; k++) weight_diff -= r.weightY[k]; if (weight_diff > 0.0) { weight = 0.0; for (k = constraints.tempY; k < py; k++) weight += r.weightY[k]; for (k = constraints.tempY; weight > 0.0 && k < py; k++) { double wt = r.weightY[k]; double dy = (wt * weight_diff) / weight; r.weightY[k] += dy; weight_diff -= dy; weight -= wt; } /* Assign the remainder to the bottom cell */ r.weightY[py - 1] += weight_diff; } /* * Calculate the minHeight array values. * First, figure out how tall the current slave needs to be. * Then, see if it will fit within the current minHeight values. * If it will not fit, add the difference according to the * weightY array. */ pixels_diff = constraints.minHeight + constraints.ipady + constraints.insets.top + constraints.insets.bottom; for (k = constraints.tempY; k < py; k++) pixels_diff -= r.minHeight[k]; if (pixels_diff > 0) { weight = 0.0; for (k = constraints.tempY; k < py; k++) weight += r.weightY[k]; for (k = constraints.tempY; weight > 0.0 && k < py; k++) { double wt = r.weightY[k]; int dy = (int) ((wt * ((double) pixels_diff)) / weight); r.minHeight[k] += dy; pixels_diff -= dy; weight -= wt; } /* Any leftovers go into the bottom cell */ r.minHeight[py - 1] += pixels_diff; } } else if (constraints.tempHeight > i && constraints.tempHeight < nextSize) nextSize = constraints.tempHeight; } } return r; } }
public void layoutContainer(Container parent) { Insets insets = parent.insets(); int ncomponents = parent.countComponents(); int nrows = getRows(); int ncols = getColumns(); int hgap = getHgap(); int vgap = getVgap(); if (nrows > 0) { ncols = (ncomponents + nrows - 1) / nrows; } else { nrows = (ncomponents + ncols - 1) / ncols; } // Set heights int x; int y; int nFills = 0; boolean[] fills = new boolean[nrows]; int lastFillRow = -1; int nComps = parent.getComponentCount(); y = insets.top; for (int row = 0; row < nrows; row++) { // Find largest minimum height for this row int h = 0; for (int col = 0; col < ncols; col++) { if (row * ncols + col < nComps) { Component c = parent.getComponent(row * ncols + col); h = Math.max(h, c.getMinimumSize().height); } } // Set heights for this row x = insets.left; for (int col = 0; col < ncols; col++) { if (row * ncols + col < nComps) { JComponent c = (JComponent) parent.getComponent(row * ncols + col); int w = c.getWidth(); c.setBounds(x, y, w, h); x += w + hgap; if (col == 0 && getFillRow(c)) { fills[row] = true; } } } y += h + vgap; if (fills[row]) { nFills++; lastFillRow = row; } } // Fill heights if (nFills > 0 && y < parent.getHeight()) { // How much height to add int hAdd = (parent.getHeight() - y) / nFills; int hAdded = 0; for (int row = 0; row < nrows; row++) { if (fills[row]) { if (row == lastFillRow) { // Compensate for rounding error hAdd = parent.getHeight() - (y + hAdded); } for (int col = 0; col < ncols; col++) { if (row * ncols + col < nComps) { Component c = parent.getComponent(row * ncols + col); Rectangle b = c.getBounds(); c.setBounds(b.x, b.y + hAdded, b.width, b.height + hAdd); } } hAdded += hAdd; } } } // Set widths nFills = 0; fills = new boolean[ncols]; int lastFillCol = -1; x = insets.left; for (int col = 0; col < ncols; col++) { // Find largest minimum width for this column int w = 0; for (int row = 0; row < nrows; row++) { if (row * ncols + col < nComps) { Component c = parent.getComponent(row * ncols + col); w = Math.max(w, c.getMinimumSize().width); } } // Set widths for this column y = insets.top; for (int row = 0; row < nrows; row++) { if (row * ncols + col < nComps) { JComponent c = (JComponent) parent.getComponent(row * ncols + col); int h = c.getHeight(); c.setBounds(x, y, w, h); y += h + vgap; if (row == 0 && getFillColumn(c)) { fills[col] = true; } } } x += w + hgap; if (fills[col]) { nFills++; lastFillCol = col; } } // Fill widths if (nFills > 0 && x < parent.getWidth()) { // How much width to add int wAdd = (parent.getWidth() - x) / nFills; int wAdded = 0; for (int col = 0; col < ncols; col++) { if (fills[col]) { if (col == lastFillCol) { wAdd = parent.getWidth() - (x + wAdded); } for (int row = 0; row < nrows; row++) { if (row * ncols + col < nComps) { Component c = parent.getComponent(row * ncols + col); Rectangle b = c.getBounds(); c.setBounds(b.x + wAdded, b.y, b.width + wAdd, b.height); } } wAdded += wAdd; } } } }