2

So I know this may be a duplicate question, but I've looked through many of the ones already on here and none of them seem to work for me, so I thought I would post my own and hopefully some of the other people having trouble with this will find this helpful also.

Here is my code

    table.getColumn("Name").setCellRenderer(
                new DefaultTableCellRenderer() {
                    @Override
                    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
                        setText(value.toString());

                        if (row==3) 
                        {
                            setForeground(Color.RED);
                        }
                        return this;
                    }
                }
            );

Here is what is displayed in the JFrame. As you can see I am trying to to only color the text in the third row of the Column "Name" but it colors the whole row. enter image description here

Any suggestions? Thanks! Canaan

mKorbel
  • 109,525
  • 20
  • 134
  • 319
k9b
  • 1,457
  • 4
  • 24
  • 54

1 Answers1

5

The render is unique for column "Name". You are setting Red as foreground color when row is 3 but you dont reset it for others rows, so when painter is called it always paint red. You have to set red when row is 3 but you also have to reset the original color in other case.

EDITED: Performed version. Now original foreground color is backed up, and super is used to render like others columns.

           table.getColumn("Name").setCellRenderer(
            new DefaultTableCellRenderer() {

        Color originalColor = null;

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            DefaultTableCellRenderer renderer = (DefaultTableCellRenderer) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
            if (originalColor == null) {
                originalColor = getForeground();
            }
            if (value == null) {
                renderer.setText("");
            } else {
                renderer.setText(value.toString());
            }

            if (row == 3) {
                renderer.setForeground(Color.RED);
            } else {
                renderer.setForeground(originalColor); // Retore original color
            }
            return renderer;
        }
    });
Ezequiel
  • 3,477
  • 1
  • 20
  • 28
  • Thanks! Worked Great! – k9b Jul 18 '13 at 20:23
  • One more question, is there a setForeground(null)? I want to set row == 3 to red but not do anything in the else{ bracket. I just want it to do nothing there basically but foreground black bolds it, thanks! Basically Color.BLACK is not the original color – k9b Jul 18 '13 at 20:27
  • 1. never to setValue inside XxxRenderer, btw JTable haven't any issue with null in cell or in XxxTableModel 2. renderer.setForeground(originalColor); instead of originalColor should be myTable.getForeground() 3. @Canaan Linder please what did you talking about not true at all good Renderer is published on this forum more than two/three times per week – mKorbel Jul 18 '13 at 20:53