File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes JTable and ScrollPane movement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable and ScrollPane movement" Watch "JTable and ScrollPane movement" New topic

JTable and ScrollPane movement

Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
I made a JTable and populated it using JDBC. I am able to scroll down through the rows using the scrollbar but I also created buttons to move the selected row up by one, down by one, to the first row, and to the last row. The problem is when I click on the last row button is does make the last row in the JTable the selected row but it doesn't move the scroll bar down to make the last row visible (I still have to move the scroll bar down). The same happens if I'm at the end of the table and I click on the first row button. Also, if I keep clicking the next row button when the selected row disappears from the viewable area it does not automatically move the scroll bar. Do I have to do this manually? Thanks.
Brett Spell
Ranch Hand

Joined: Feb 24, 2001
Posts: 107
No, you don't have to do it manually. You just need to do two things: obtain a Rectangle that identifies the coordinates and size of a cell that you want to make visible, and then use JViewport's scrollRectToVisible() method to make that cell visible. (The JViewport can be accessed by calling the JScrollPane's getViewport() method). For example, let's suppose that you've

This assumes that you aren't concerned with horizontal scrolling, but even if you are, you can use the same technique with very little additional code.
Brett Spell
Author, Professional Java Programming
I agree. Here's the link:
subject: JTable and ScrollPane movement
It's not a secret anymore!