Dimension (@NotNull Component component) { Dimension size = component.getPreferredSize(); if (size == null) return new Dimension(); // rare if (component.isMaximumSizeSet()) { Dimension max = component.getMaximumSize(); if (max != null) { if (size.width > max.width) size.width = max.width; if (size.height > max.height) size.height = max.height; } } return size; }