JScrollPane autoscroll?

Question 1:
I put a JTable into a JScrollPane and would like to ensure the JTable always scrolled to the BOTTOM automatically.How should I code this?

Question 2:
Besides this, I have a similar porblem but instead a JTable, I am putting a custom line graph component that extends JPanel. How could I ensure that the graph always scrolled to the RIGHT automatically.

Who is Participating?
objectsConnect With a Mentor Commented:
1. table.scrollRectToVisible(table.getCellRect(table.getRowCount(), 0, false));

2. Similar approach, passing the regoin of the panel you want visible.

b612_foreverAuthor Commented:
I always think that the solution should come from the JScrollPane so I can't find the solution.

Thank you very much for your help.
Here's a method you could use (a solution using JScrollPane):
public void autoScroll(JScrollPane sPane){
        JScrollBar vScroll = sPane.getVerticalScrollBar();
You can include this method in your class and call it by passing the scroll pane object that is holding your table. Call this method to the event in the same place as where the table contents are initialized.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.