Dimension (Container parent) { if (parent instanceof JViewport viewport) { Dimension size = getPreferredScrollableViewportSize(viewport.getView()); if (size != null) return size; // may be null for for tables or custom components } return new Dimension(); }