Search code examples
javaswingclientsizejscrollpane

Resize JScrollPane client without scrollbars rolling


I have a JScrollpane which contains a scrollable client that changes its size dynamically while using the application. I want the JScrollPane to be aware of the changes without moving the viewport when the client size changes.

To clarify what I mean: Refer to the Java Webstart example ScrollDemo2 from the article How to use scroll panes by Sun. When clicking at the bottom of the window, a circle appears partly outside the window and the scrollbars move. It's the latter behavior I want to avoid.

My guess is that it's just a matter of setting a simple flag in one of the many components that are involved in a scroll pane solution, but I just can't find where it is. Does anyone know?


Solution

  • I managed to solve this problem by overriding the standard behavior of the viewport in my JScrollPane. This might be a solution that is not suitable for all, but in my GUI this works like a charm.

    JScrollPane pane = new JScrollPane();
    pane.setViewport(
      new JViewport(){
        /**
         * An empty override implementation to prevent undesired scrolling on
         * size changes of the client.
         */
         @Override
         public void scrollRectToVisible(Rectangle rect){}
      });