int (int widthToFit, int maxWidth) { int componentCount = getComponentCount(); // Calculate max size of a row. It's not possible to make more than 3 row toolbar int maxRowWidth = Math.max(widthToFit, componentCount * maxWidth / 3); for (int i = 0; i < componentCount; i++) { final Component component = getComponent(i); if (component instanceof JComponent && ((JComponent)component).getClientProperty(RIGHT_ALIGN_KEY) == Boolean.TRUE) { maxRowWidth -= getChildPreferredSize(i).width; } } return maxRowWidth; }