2

I have created a custom cell renderer class to achieve this.

public class MatchTableCellRenderer extends DefaultTableCellRenderer{


    public Component getTableCellRendererComponent (JTable table,
                                                    Object obj, boolean isSelected, boolean hasFocus, int row, int column) {
        Component cell;
        cell = super.getTableCellRendererComponent(
                table, obj, isSelected, hasFocus, row, column);
        if( ((String[]) ((MatchTableModel) table.getModel()).getRow(row)).length==7 ){
            System.out.println(((String[]) ((MatchTableModel) table.getModel()).getRow(row))[0]+" "+((String[]) ((MatchTableModel) table.getModel()).getRow(row))[6]);

            cell.setForeground(Color.green);

        }
        return cell;
    }


}

And I have set this renderer to be used by my table's columns:

    tempColumn = table.getColumnModel().getColumn(0);
    tempColumn.setCellEditor(new MacColumnEditor());
    tempColumn.setCellRenderer(new MatchTableCellRenderer());

    tempColumn = table.getColumnModel().getColumn(1);
    tempColumn.setCellEditor(new IpColumnEditor());
    tempColumn.setCellRenderer(new MatchTableCellRenderer());

    tempColumn = table.getColumnModel().getColumn(2);
    DefaultCellEditor dfEditor=new DefaultCellEditor(new JTextField());
    dfEditor.setClickCountToStart(2);
    tempColumn.setCellEditor(dfEditor);
    tempColumn.setCellRenderer(new MatchTableCellRenderer());

I want the rows which contain a String[] of length=7 green and the others with the default color. But it is interesting that all my rows become green. I have a print line as you can see. It is printed 4 times (my table has 12 rows), but all rows are made green, instead of 4. What am I doing wrong?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Alptugay
  • 1,676
  • 4
  • 22
  • 29

2 Answers2

3

You can use XxxCellRenderer, but better and easiest is to use prepareRenderer()

for correct code you have to override or test inside if-else follows patameters

  • isSelected

  • hasFocus

  • column

  • row

more in answers and question about similair issue

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • @MadProgrammer prepareRenderer as such is not a good idea, except if a) the highlight should be applied for a complete row b) a well-designed (biased me, again) pluggable mechanism in place to fully control what/when/how to decorate the component - as f.i. in SwingX :-) The base rule is to no subclass any of the JSomething for application reasons – kleopatra Jul 27 '12 at 10:29
  • +1 Thanks for the answer. prepareRenderer() helped me but I achieved what I want with the answer of @kleopatra – Alptugay Jul 27 '12 at 11:01
  • @kleopatra while I would not disagree with, I would argue that it comes down to the requirements at hand – MadProgrammer Jul 27 '12 at 12:18
  • @MadProgrammer sure, it always does :-) but the OP's don't fall into a category which justifies breaking a base rule. Anyway, the _problem_ the OP is seeing is independent on _where_ the coloring is done, depends only on _how_ – kleopatra Jul 27 '12 at 13:09
  • @kleopatra given the amount of information, knowing exactly what's required is a difficult call to make, whie I agree with you answer & I would certinly encourage it over extending the base class, having the information to make an informed decision is also important (which is what I like about you) - its not "you're an idiot", it's "you're an idiot & here's why" ;) - that's just MHO. I expect & look forward to you keeping me on my toes! Also, personally, I would have made the renderer transparent if no color was required, but that's just me though ;) – MadProgrammer Jul 27 '12 at 13:19
3

The reason is the infamous color memory (TM) of the DefaultTableCellEditor: you have to set the colors always, instead of only in one branch.

if (myCondition) {
   setBackground(...) {
} else {
   setBackground(...) 
} 

the exact details are explained in a recent thread

Community
  • 1
  • 1
kleopatra
  • 51,061
  • 28
  • 99
  • 211
  • The thread you gave me helped a lot. Thank you. Now I have one more problem. I need the highlight to work properly after the table is sorted :/ – Alptugay Jul 27 '12 at 11:02