We help IT Professionals succeed at work.

Looping through and selecting rows in a Swing GUI JTable component

gdkinney_2
gdkinney_2 asked
on
Medium Priority
3,271 Views
Last Modified: 2008-03-03
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
Comment
Watch Question

Top Expert 2006

Commented:
Use table.changeSelection(row, col, false, false) for each cell you want to be selected
Top Expert 2006

Commented:
Sr. Software engineer
CERTIFIED EXPERT
Commented:
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);
}

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
Thanx 4 axxepting

Author

Commented:
zzynx no problem it's what I needed.

Thanks!
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.