shree
shree

Reputation: 2757

How to check values are unique in JTable?

I have a JTable which has two columns and 10 rows. When I read second column values from the JTable, I have to check whether those are unique values.

How to check?

Upvotes: 1

Views: 2310

Answers (1)

Rajeev Sreedharan
Rajeev Sreedharan

Reputation: 1753

You could use a Set to determine if duplicates are being added.

TreeSet set = new TreeSet();

TableModel tableModel = table.getModel() ;
for(int i=0; i<tableModel.getRowCount();i++){
    Object obj = tableModel.getValueAt(i, 2);
    if(!set.add(obj)){
        //throw duplicate error
    }
}

Upvotes: 8

Related Questions