Dimension (Dimension preferredSize) { final ArrayList<Rectangle> bounds = new ArrayList<>(); int forcedHeight; if (getWidth() > 0 && getLayoutPolicy() == ActionToolbar.WRAP_LAYOUT_POLICY && myOrientation == SwingConstants.HORIZONTAL) { calculateBounds(new Dimension(getWidth(), Integer.MAX_VALUE), bounds); Rectangle union = null; for (Rectangle bound : bounds) { union = union == null ? bound : union.union(bound); } forcedHeight = union != null ? union.height : 0; } else { calculateBounds(new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE), bounds); // it doesn't take into account wrapping forcedHeight = 0; } if (bounds.isEmpty()) return JBUI.emptySize(); int xLeft = Integer.MAX_VALUE; int yTop = Integer.MAX_VALUE; int xRight = Integer.MIN_VALUE; int yBottom = Integer.MIN_VALUE; for (int i = bounds.size() - 1; i >= 0; i--) { final Rectangle each = bounds.get(i); if (each.x == Integer.MAX_VALUE) continue; xLeft = Math.min(xLeft, each.x); yTop = Math.min(yTop, each.y); xRight = Math.max(xRight, each.x + each.width); yBottom = Math.max(yBottom, each.y + each.height); } final Dimension dimension = new Dimension(xRight - xLeft, Math.max(yBottom - yTop, forcedHeight)); if (myLayoutPolicy == AUTO_LAYOUT_POLICY && myReservePlaceAutoPopupIcon && !isInsideNavBar()) { if (myOrientation == SwingConstants.HORIZONTAL) { dimension.width += AllIcons.Ide.Link.getIconWidth(); } else { dimension.height += AllIcons.Ide.Link.getIconHeight(); } } JBInsets.addTo(dimension, getInsets()); return dimension; }