Tutorial :Initializing a Java Swing JScrollPane to the bottom



Question:

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:1

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.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »