• Post Reply Bookmark Topic Watch Topic
  • New Topic

JScollPane autoscroll

 
Olivier Legat
Ranch Hand
Posts: 176
Chrome Mac Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,

Does anyone know a simple way to set the y scrollbar of JScrollPane automatically to the very bottom? So as to allow auto-scrolling.

Thank-you.
 
Rob Spoor
Sheriff
Posts: 20822
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Depending on the component inside you may want to use methods of that component. For example, setting the caret position of a JTextArea to the end of its text will scroll to the end. JList has method ensureIndexIsVisible and JTree has scrollPathToVisible and scrollRowToVisible. JComponent has scrollRectToVisible which you can easily combine with JTable's getCellRect method.

Now, if you don't have one of these components, or cannot calculate the rectangle to scroll to, there is a very easy way:
- retrieve the JScrollPane's vertical JScrollBar
- set that JScrollBar's value to its maximum (hint: those are JavaBean property names)
This will work with any JScrollPane and any viewport component.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!