void (@NotNull Dimension sizeToFit, @NotNull List<? extends Rectangle> bounds) { // We have to graceful handle case when toolbar was not laid out yet. // In this case we calculate bounds as it is a NOWRAP toolbar. if (getWidth() == 0 || getHeight() == 0) { try { setLayoutPolicy(NOWRAP_LAYOUT_POLICY); calculateBoundsNowrapImpl(bounds); } finally { setLayoutPolicy(WRAP_LAYOUT_POLICY); } return; } final int componentCount = getComponentCount(); LOG.assertTrue(componentCount <= bounds.size()); final Insets insets = getInsets(); int widthToFit = sizeToFit.width - insets.left - insets.right; int heightToFit = sizeToFit.height - insets.top - insets.bottom; if (myAdjustTheSameSize) { final int maxWidth = getMaxButtonWidth(); final int maxHeight = getMaxButtonHeight(); int xOffset = 0; int yOffset = 0; if (myOrientation == SwingConstants.HORIZONTAL) { // Lay components out int maxRowWidth = getMaxRowWidth(widthToFit, maxWidth); for (int i = 0; i < componentCount; i++) { if (xOffset + maxWidth > maxRowWidth) { // place component at new row xOffset = 0; yOffset += maxHeight; } final Rectangle each = bounds.get(i); each.setBounds(insets.left + xOffset, insets.top + yOffset, maxWidth, maxHeight); xOffset += maxWidth; } } else { // Lay components out // Calculate max size of a row. It's not possible to make more then 3 column toolbar final int maxRowHeight = Math.max(heightToFit, componentCount * myMinimumButtonSize.height() / 3); for (int i = 0; i < componentCount; i++) { if (yOffset + maxHeight > maxRowHeight) { // place component at new row yOffset = 0; xOffset += maxWidth; } final Rectangle each = bounds.get(i); each.setBounds(insets.left + xOffset, insets.top + yOffset, maxWidth, maxHeight); yOffset += maxHeight; } } } else { if (myOrientation == SwingConstants.HORIZONTAL) { // Calculate row height int rowHeight = 0; final Dimension[] dims = new Dimension[componentCount]; // we will use this dimensions later for (int i = 0; i < componentCount; i++) { dims[i] = getChildPreferredSize(i); final int height = dims[i].height; rowHeight = Math.max(rowHeight, height); } // Lay components out int xOffset = 0; int yOffset = 0; // Calculate max size of a row. It's not possible to make more then 3 row toolbar int maxRowWidth = getMaxRowWidth(widthToFit, myMinimumButtonSize.width()); for (int i = 0; i < componentCount; i++) { final Dimension d = dims[i]; if (xOffset + d.width > maxRowWidth) { // place component at new row xOffset = 0; yOffset += rowHeight; } final Rectangle each = bounds.get(i); each.setBounds(insets.left + xOffset, insets.top + yOffset + (rowHeight - d.height) / 2, d.width, d.height); xOffset += d.width; } } else { // Calculate row width int rowWidth = 0; final Dimension[] dims = new Dimension[componentCount]; // we will use this dimensions later for (int i = 0; i < componentCount; i++) { dims[i] = getChildPreferredSize(i); final int width = dims[i].width; rowWidth = Math.max(rowWidth, width); } // Lay components out int xOffset = 0; int yOffset = 0; // Calculate max size of a row. It's not possible to make more then 3 column toolbar final int maxRowHeight = Math.max(heightToFit, componentCount * myMinimumButtonSize.height() / 3); for (int i = 0; i < componentCount; i++) { final Dimension d = dims[i]; if (yOffset + d.height > maxRowHeight) { // place component at new row yOffset = 0; xOffset += rowWidth; } final Rectangle each = bounds.get(i); each.setBounds(insets.left + xOffset + (rowWidth - d.width) / 2, insets.top + yOffset, d.width, d.height); yOffset += d.height; } } } }