I want to have a jtable which contains unique row data's.if two or more row data's are same then i want to change those rows background color.i found that custom table cell rendering is the best option to do these operation,but i don't know how to use custom cell rendering,please guide me.
here is my code:
ArrayList<String> dup1 = new ArrayList<String>();
int dup_flag=0;
for(int count = 0; count < getTablXtocsvXB_B3_B3_1().getRowCount(); count++){
String dup=(getTablXtocsvXB_B3_B3_1().getValueAt(count, 0).toString());
if(dup1.contains(dup)){
dup_flag=1;
//have to color this row since it is a duplicate one
getTablXtocsvXB_B3_B3_1().getColumnModel().getColumn(0).setCellRenderer(new colorRenderer());
else {
dup1.add(dup);
}
}
Here is the custom cell rendering code:
public class colorRenderer extends DefaultTableCellRenderer {
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
setBackground(Color.RED);
return this;
}
}
I don't know whether the custom cell rendering code is correct and also don't know how to merge these two code.I'm new to this custom cell rendering,pls suggest some code to do this operation.is there any other alternative to do this?