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 positioning row at top 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 positioning row at top" Watch "JTable positioning row at top" New topic

JTable positioning row at top

Graham Mead
Ranch Hand

Joined: Sep 28, 2001
Posts: 57
Hi I'm trying to develop a JTable so that I can position a specific row at the TOP of the JTable.
I've seen examples using scrollRectToVisible() but this just makes the row visible but if the row is below the viewport it puts the row at the bottom not the top.
If the row is above the viewport then it works ok.
I've got it to work by issuing the scrollRectToVisible() method twice. Once with a v large row number then the actual one to force the row above the viewport on the first call. This seems very clunky/inefficient though!
Does anyone have any ideas.
As an extension to this I also want to position a row to the top even if the view is large enough to display all the rows, hence no scrollbar.
TIA Graham
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Graham,
Why not do it with the table model? Whatever row it is you want displayed at the top, could be returned in the getValueAt method of the table model. You would of course have to keep up with that row in the model.
Hope this helps,
Michael Morris

Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
I agree. Here's the link:
subject: JTable positioning row at top
jQuery in Action, 3rd edition