Reputation: 39
I want to search data from JTable
when data found then I want to highlight table row. This code is work properly search record but I don't know what I do for highlight the row.
String target = jTextField1.getText();
for(int row = 0; row < jTable1.getRowCount(); row++)
for(int col = 0; col < jTable1.getColumnCount(); col++)
{
String next = (String)jTable1.getValueAt(row, col);
if(next.equals(target))
{
System.out.println("found");// here what change for highlight row.
}
}
Upvotes: 0
Views: 1289
Reputation: 150
We can achieve that with a custom JLabel and TableCellRenderer. Following example does the highlighting on the found (filtered) rows in JTable. The rows are filtered via RowFilter: http://www.logicbig.com/tutorials/core-java-tutorial/swing/jtable-row-filter-highlighting/
Upvotes: 1
Reputation: 61
else if(e.getSource()==field){
int z;
for(z = 0;z<table.getRowCount();z++){
if(Integer.parseInt(field.getText()) == Integer.parseInt((String)table.getValueAt(z, 1))){
break;
}
}
table.setRowSelectionInterval(z, z);
}
i had the same problem and this was how i went about it.
Upvotes: 0
Reputation: 347204
The answer depends on your idea of "highlighting"
You could use JTable#addRowSelection
to highlight a row using the default selection
Or, you could setup your cell renders to apply additional highlighting support via an additional lookup to determine if the cell/row should be highlighted
Or, you could use the inbuilt filtering capabilities of the JTable
to filter out unwanted content
See How to use tables for more details
Or, you could use the highlighting support from the SwingLabs, SwingX librRies
Upvotes: 1