dtnder
dtnder

Reputation: 389

display last row in JTable,Java

I have a JTable. Maximum row displayed only four, while the number of all rows of eight. I want when the program starts the table will directly lead to the last row.

here's when the program is start :

enter image description here

I want to like this when program is start :

enter image description here

table directly leads to the last row

Upvotes: 0

Views: 2797

Answers (2)

Syed Muhammad Mubashir
Syed Muhammad Mubashir

Reputation: 1016

You can also do this with the following code

  int lastRow=table.getRowCount-1;

  table.setCellSelectionEnabled(true);
  table.changeSelection(lastRow, 0, false, false);
  table.scrollRectToVisible(new Rectangle(tblDetailInfo.getCellRect(lastRow, 0, true)));

Upvotes: 2

MadProgrammer
MadProgrammer

Reputation: 347294

You need to get the cell bounds for the last row, JTable#getCellRect(int, int, boolean) will return the rectangle bounds of a given cell, so something like;

Rectangle cellBounds = table.getCellRect(table.getRowCount() - 1, 0, true);

Should give you the location of the given cell.

Armed with this information, you simply need to call JComponent#scrollRectToVisible(Rectangle) to request that the given rectangle is made visible.

table.scrollRectToVisible(cellBounds);

Upvotes: 4

Related Questions