Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

comparing a string value with a jtable value  RSS feed

 
shama parween
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my program i have to read a text from JTextfield and comapare it with the values from JTable.
Once the values is found that particular row in the JTable has to get highlighted.
i have used the following code but its not working.

int rowcount=table.getRowCount();
int colcount=table.getColumnCount();
String str=textfield.getText();
for(int i=0;i<rowcount;i++)
{
for(int j=0;j<colcount;j++)
{
if(table.getValueAt(i,j).toString().equals(str))
table.setRowSelectionInterval(i,j);
}
}
Please help me with this.
 
Rob Spoor
Sheriff
Posts: 21092
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's not working? Is nothing selected? Or only a single row?

If the latter is the case, you might want to avoid using setRowSelectionInterval - that will result in only that row being selected.

Instead, you'll want to call clearSelection before the loops, then addRowSelectionInterval in the loop.


Another approach is use a table cell renderer for this:
 
shama parween
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply rob. Actually the probelm is the control is not coming inside the if condition.Whatever the value is it always returns false.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!