Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Cell Selection In JTable  RSS feed

 
Isaac Hewitt
Ranch Hand
Posts: 191
Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can I set a cell selected at the top of the visible part of a JTable if the scrollbar position is not at zero.
I am well aware of this method: this.table.changeSelection(0, 0, false, false);
However I cannot detect the number of the cell at the top of the visible part of the table. There is a method for the JLsit that does this.
Would anyone know of a workaround?
Thanks.
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can retrieve where the user has scrolled to by getting the JScrollPane's JViewport, then calling its methods getViewPosition, getViewRect and getViewSize. If you have those you can calculate which cell is visible by going through the cells and checking their getCellRect return values.
 
Isaac Hewitt
Ranch Hand
Posts: 191
Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How daft of me. Thanks Rob.
 
Rob Camick
Ranch Hand
Posts: 2753
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rather than looping through all the cells you can just use the rowAtPoint(...) method once you know the viewport start position.
 
Isaac Hewitt
Ranch Hand
Posts: 191
Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your additional comment Rob Camick
 
Isaac Hewitt
Ranch Hand
Posts: 191
Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It works extactly as I wished with this code:



Thank you to the both of you for your input.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!