Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTable and ScrollPane movement

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Richard,
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic