Component comp = ev.getComponent(); Dimension newSize = comp.getSize(); Dimension viewportSize = getSize(); boolean revert = false; if (newSize.width > viewportSize.width) { newSize.width = viewportSize.width; revert = true; } if (newSize.height > viewportSize.height) { newSize.height = viewportSize.height; revert = true; } if (revert == true) comp.setSize(newSize);
revalidate();
    public void componentResized(ComponentEvent ev)    {      // According to some tests that I did with Sun's implementation      // this class is supposed to make sure that the view component      // is not resized to a larger size than the viewport.      // This is not documented anywhere. What I did is: I subclassed JViewport      // and ViewListener and 'disabled' the componentResized method by      // overriding it and not calling super.componentResized().      // When this method is disabled I can set the size on the view component      // normally, when it is enabled, it gets immediatly resized back,      // after a resize attempt that would exceed the Viewport's size.      Component comp = ev.getComponent();      Dimension newSize = comp.getSize();      Dimension viewportSize = getSize();      boolean revert = false;      if (newSize.width > viewportSize.width)        {          newSize.width = viewportSize.width;          revert = true;        }      if (newSize.height > viewportSize.height)        {          newSize.height = viewportSize.height;          revert = true;        }      if (revert == true)        comp.setSize(newSize);    }