public Dimension getPreferredScrollableViewportSize() { Dimension size = getPreferredSize(); Dimension screenSize = PortingUtils.getLocalScreenSize(this); Container container = SwingUtilities.getAncestorOfClass(SimpleScrollPane.class, this); if (container instanceof SimpleScrollPane) { SimpleScrollPane scrollPane = (SimpleScrollPane) container; int height = screenSize.height; // limit it to the height determined by the visible menu item count if (getVisibleMenuItemCount() > 0) { int totalHeight = getVisibleMenuItemCount() * getScrollableUnitIncrement(null, 0, 0); if (height > totalHeight) { height = totalHeight; } } size.height = Math.min(size.height, height - scrollPane.getScrollUpButton().getPreferredSize().height - scrollPane.getScrollDownButton().getPreferredSize().height); } return size; }