Search code examples
javaswingjscrollpane

Initializing a Java Swing JScrollPane to the bottom


I am trying to initialize a JScrollPane to start life at the bottom. I do not want it to scroll automatically after it is initially shown. The scroll pane does not contain a subclass of JTextComponent, but rather a JPanel(GridLayout(0, 1)) containing many JPanels.

I tried using JViewport.scrollRectToVisible() inside an event handler on the parent Window (addComponentListener:componentShown), but it did not seem to work.

Any ideas?


Solution

  • The scroll pane does not contain a subclass of JTextComponent, but rather a JPanel(GridLayout(0, 1)) containing many JPanels.

    Then you need to scroll the panel:

    panel.scrollRectToVisible(...);
    

    Or you should be able to use:

    JScrollBar sb = scrollPane.getVerticalScrollBar();
    sb.setValue( sb.getMaximu() );
    

    Also, this code needs to be executed "after" the GUI is visible.