0

I have created a JTable with the help of AbstractTableModel. I would like to mark (change the color of the cell) in a this JTable the third column which has the same entry as the second column. For example:

enter image description here

and so, with the help of this post Check duplicate data in jtable before proceeding

I have came to this:

      table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer()
      {
         @Override
         public Component getTableCellRendererComponent(JTable table,
               Object value, boolean isSelected, boolean hasFocus, int row,
               int column) {
            Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus,
                  row, column);

            TreeSet<Object> set = new TreeSet<Object>();

            for (int i=0; i<model.getRowCount();i++){
                Object obj = model.getValueAt(i,1); //(row, column)
                if(!set.add(obj))
                {
                    c.setBackground(new java.awt.Color(255, 72, 72));
                }
                else{
                    c.setBackground(null);
                }
            }

            return c;
        }
      });

But, although it seems that it correctly checks for each row (due to some printing I have done), nothing is colored. I would like to add, that the JTable that I have created is a JTable that always changes, meaning there are two buttons, for PREVIOUS and NEXTand this table always changes each form - also the number of the columns changes.

If anything more is requested, I would edit my post.

Thank you.

Community
  • 1
  • 1
Dimitra Micha
  • 2,089
  • 8
  • 27
  • 31
  • 2
    [no idea, mabye here are better answers](http://stackoverflow.com/questions/7132400/jtable-row-hightlighter-based-on-value-from-tablecell), could be possible, but for better help sooner post an SSCCE, short, runnable, compilable, hardcode array for AbstractTableModel as local variable – mKorbel Mar 07 '13 at 09:58
  • how is the image related to your question? duplicate columns vs rows, foreground vs. background? If it isn't pleasse delete. And best to show an SSCCE that demonstrates the problem. And beware of [DefaultTableCellRenderer's infamous color memory](http://stackoverflow.com/a/9617446/203657): set the colors (your highlight or null) _before_ calling super. – kleopatra Mar 07 '13 at 10:42
  • @kleopatra the image is used to demonstrate not the way I mark the duplicates, but how my current JTable is and what I want to check. – Dimitra Micha Mar 07 '13 at 11:03
  • from the image I would guess that you want to mark all cell in a column when all values are the same as the corresponding cells in a previous colum .. which is _not_ what your logic does :-) – kleopatra Mar 07 '13 at 11:10
  • what I want to do is to check the column b and see if column c and d have the same values. – Dimitra Micha Mar 07 '13 at 11:20

1 Answers1

2

I would replace the for in the renderer with

if (column==2) {
    Object obj = model.getValueAt(row,column-1); //(row, column)
    if(value.equals(obj)) {
       c.setBackground(new java.awt.Color(255, 72, 72)); //red
    }
    else{
       c.setBackground(null);
    }
}
StanislavL
  • 56,971
  • 9
  • 68
  • 98
  • I guess this is correct if the JTable has only two columns right? And even if it has only two columns, it marks the 3 cells continuing to the next row. – Dimitra Micha Mar 07 '13 at 14:12