0

I have created a JTable, added it to the JScrollpane and added a method

public boolean getScrollableTracksViewportHeight() {
  if ( getParent() instanceof JViewport ) {
    return ( ( (JViewport) getParent() ).getHeight() > getPreferredSize().height );
  }
  return false;
}

I also added table.scrollRectToVisible(table.getCellRect(30, 0, true));

However, the scrollbar is at the top of the table. Is there a way so that I can move it to end of the table always when opening a table?

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
user2345736
  • 49
  • 1
  • 6

1 Answers1

3

You may mean that you want the scrollbar's indicator, sometimes called the thumb, to move to the bottom of the scrollbar as needed. There's a complete example here. In summary,

int last = table.getModel().getRowCount() - 1;
Rectangle r = table.getCellRect(last, 0, true);
table.scrollRectToVisible(r);

image

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • no i am not adding row at runtime.I am selecting some values from my GUI and it will be displayed in tabular format.I want always scrollbar at last row.how can i do it? – user2345736 Jul 03 '13 at 04:55
  • Do it exactly the same way in your constructor, _after_ you create your scroll pane, of course. – trashgod Jul 03 '13 at 09:41