Dimension () { if (isPreferredSizeSet()) { return super.getPreferredSize(); } Dimension preferredSize = super.getPreferredSize(); return new Dimension(Math.min(400, preferredSize.width), preferredSize.height); }