Forum Discussion

AKarandjeff's avatar
AKarandjeff
Contributor
13 years ago

Determining if table rows are selected

I have a test scenario where I need to check whether or not a Select All button actually selects all of the rows within a JTable (and vice versa that Select None deselects all the rows).  JTables do not appear to have a selected row property, so was wondering if anyone had a suggestion on how to do the validation.  Would do a region checkpoint, but am concerned that the selected color might change, causing the checkpoint to fail.  Thanks.

4 Replies

  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)
    Hi Andrew,



    You can get the total number of the selected rows in JTable and the selected row indexes using JTable's native getSelectedRowCount and getSelectedRows methods. So, for example, you can perform the validation by comparing the return value of the getSelectedRowCount method with the total number of table rows:

    if (table.getSelectedRowCount() == table.wRowCount)

    {

      // Checkpoint passed: All rows are selected

    }

    else

    {

      // Checkpoint failed: Not all rows are selected

    }
  • OK, I can get this to work, but my selectedRowCount is coming out to 11 though my table row count is only 10.  I've not been able to find any information on whether or not the method counts the header row, or possibly I have a hidden row that I'm not aware of.  Any thoughts?  Pretty cool, though, thanks!
  • I think I figured out why the counts were different.  The Select All button performs a selection interval starting 0 and uses the getRowCount method to set the upper end. Being base 0, the getRowCount number of 10 is actually the 11th counted row.  I was able to go into our application code and changed the Select All button's selection interval to be from 0 to getRowCount - 1 and the getSelectedRows returned 10.