Looping through and selecting rows in a Swing GUI JTable component

I want to loop through the rows of a JTable and select one or more of the rows based on a unique value (ID) for the row.  Also I would like to hide the ID column in the row of the JTable so that it is not displayed in the rows of the JTable.  How do I accomplish this?

Thank You,
Dave
gdkinney_2Asked:
Who is Participating?
 
zzynxSoftware engineerCommented:
int yourIDColumnIndex = 0; // I assume that your ID column is the first in the table
int theIDYouWantToSelect = 15; // Assume you want to select the rows with ID=15
int totalRows = theTable.getRowCount();
theTable.clearSelection();
for (int row=0; row < totalRows; row++) {
   Integer id = (Integer)theTable.getValueAt(row, yourIDColumnIndex);
   if (id.intValue() == theIDYouWantToSelect)
      theTable.addRowSelectionInterval(row, row);
}
0
 
hoomanvCommented:
Use table.changeSelection(row, col, false, false) for each cell you want to be selected
0
 
zzynxSoftware engineerCommented:
Thanx 4 axxepting
0
 
gdkinney_2Author Commented:
zzynx no problem it's what I needed.

Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.