Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JTable: scroll to get the entered row number at the top of screen

Status
Not open for further replies.

rach18

Programmer
Feb 6, 2009
9
0
0
US
I have a Jtable. Upon accepting the row number entered by the user, the query results screen shall scroll down such that the entered row number is "at the top of the screen" of JTable.

The code i have used is:

//Dialog box to fetch row number from the user
rowNo = customDialog.getRowNum();
if (rowNo > 0)
{
//Scroll to the first row of jTable
jTable.scrollRectToVisible(jTable.getCellRect(0,0,true));

//Scroll such that entered row no. is at the top of the screen
jTable.setRowSelectionInterval(rowNo-1,rowNo-1);
int y1 = (rowNo-1) * jTable.getRowHeight();
int w1 = jTable.getWidth();
int h1 = jTable.getHeight();
jTable.scrollRectToVisible(new Rectangle(0,y1,w1,h1));

}

Using the above code is working fine except for one issue. My JTable screen accomdates 20 rows at once and on maximizing, 30 rows are visible. Assume there are total of 1700 rows, and the user enters 1699. If this is the scenario, we get the last screen but the row 1699 is not at the top of the screen. So my question is if the entered row number by the user is one among last 20 or 30 rows among the total number of rows, how to get this at the top of the screen of JTable.

Any help would be appreciated.


 
Pad your data with extra blank rows?

Never used JTable myself, so this is just a guess.
 
How about this approach ...?

Use getRowHeight() on your JTable and multiply by the index of the row you want to be at the top. This gets you the number of pixels from the top of the JTable to that row.

Then use the setViewPosition(Point p) method of JViewport to force the scroll to that row.
Code:
  int topRow = 25; //or whatever
  int pRow = jTable.getRowHeight();
  jScrollpane.getViewPort().setViewPosition(new Point(0, topRow * pRow));

I apologise that I don't have time to try this out for you .. it's straight from the Apidocs.

Tim
 
*Bump*

It would be useful if you could update this thread with your progress. Did my suggestion work? If you solved it some other way, it would benefit this forum to know.

Tim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top