0

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?

mariz
  • 509
  • 1
  • 7
  • 13
  • 1
    Your best bet is to show us more code, specifically a [mcve] or [sscce](http://sscce.org). This is a small but complete (i.e., compilable and runnable) program that only has code necessary to demonstrate your problem, again that we can copy, paste, compile and run without modification, and that is posted here with your question and not in a link. Also, I see that you've got some rendering code, but haven't shown us how you've tried to integrate it with your program. Please show us that as well. Evan an incorrect attempt will give us useful information. – Hovercraft Full Of Eels Jul 13 '16 at 13:05
  • Just to amplify: we definitely don't want to see your whole program, and we don't want un-runnable snippets. Rather we want something in between. Please check the useful links above out for more details. – Hovercraft Full Of Eels Jul 13 '16 at 13:06
  • Also your logic is wrong. You set the column renderer for the complete column and when setting the renderer you don't check for duplicates. Rather the dup check is done **inside the renderer itself**. – Hovercraft Full Of Eels Jul 13 '16 at 13:21
  • In other words, don't set the renderer in the for loop or if block as you have posted above, but rather set the renderer in the code block where you create your JTable. An if block will need to be within the renderer, one that compares the `value` Object parameter passed into the getTableCellRendererComponent with other values in the same column but not the same row. – Hovercraft Full Of Eels Jul 13 '16 at 13:54
  • your code check only sequential duplicates. As i understand from your question you want a full data check. Is it right ? – mfidan Jul 13 '16 at 14:50

0 Answers0