void (final JComponent c, final Rectangle r, final boolean withInsets) { final Rectangle visible = c.getVisibleRect(); visible.x = r.x - (visible.width - r.width) / 2; visible.y = r.y - (visible.height - r.height) / 2; Rectangle bounds = new Rectangle(c.getWidth(), c.getHeight()); if (!withInsets) JBInsets.removeFrom(bounds, c.getInsets()); if (visible.x < bounds.x) { visible.x = bounds.x; } if (visible.x + visible.width > bounds.x + bounds.width) { visible.x = bounds.x + bounds.width - visible.width; } if (visible.y < bounds.y) { visible.y = bounds.y; } if (visible.y + visible.height > bounds.y + bounds.height) { visible.y = bounds.y + bounds.height - visible.height; } c.scrollRectToVisible(visible); }