views:

231

answers:

1

So I have custom CellEditors and CellRenderers and although I am doing

  component.setForeground(isSelected ? table.getSelectionForeground() : table.getForeground());
  component.setBackground(isSelected ? table.getSelectionBackground() : table.getBackground());
  component.setOpaque(true);

in my getTableCellRendererCompoent, the colors only match on every OTHER row, since most of hte look and feels I've tried seem to alternate them. How can I pull the color values in a way that they'll match whatever the rest of the table looks like? I'd also really like to be able to make nice borders to match the renderers descended from DefaultTableCellRenderer.

I've tried to dissect the DefaultTableCellRenderer, and I got lost trying to track down this UI object. Do I just need the right properties to pull from the UIManager? A lead in the right direction will be much appreciated.

Thanks all, this site rocks. Joshua

+1  A: 

Yes, you should use the Swing palette colors. For example:

final Color tableBackground = javax.swing.UIManager.getDefaults().getColor("Table.background");

Here's the color key values for tables:

  • Table.background Table.dropLineColor
  • Table.dropLineShortColor
  • Table.focusCellBackground
  • Table.focusCellForeground
  • Table.foreground Table.gridColor
  • Table.selectionBackground
  • Table.selectionForeground
  • Table.sortIconColor
  • TableHeader.background
  • TableHeader.focusCellBackground
  • TableHeader.foreground

Alternatively, you could also use the system colors. For example:

Normal background: SystemColor.window
Selected background: SystemColor.textHighlight
Normal foreground: SystemColor.text
Selected foreground: SystemColor.textText

Edward Stembler