简体   繁体   中英

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 JPanel s.

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

Any ideas?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM