Ralph
Ralph

Reputation: 32284

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?

Upvotes: 0

Views: 1749

Answers (1)

camickr
camickr

Reputation: 324118

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.

Upvotes: 1

Related Questions