7

I need to highlight every other row in my JTable. With old version of swingx it could be done like this:

table.setHighlighters(new HighlighterPipeline(new Highlighter[] { new AlternateRowHighlighter(
            color1, color2,color3 }));

but now, with swingx 1.6, method setHighlighters() can't accept those parameters. It says "The method setHighlighters(Highlighter...) in the type JXTable is not applicable for the arguments (HighlighterPipeline)"

So how can i do it with new swingx?

Kevin Reid
  • 37,492
  • 13
  • 80
  • 108
Dakarth
  • 83
  • 7
  • Can you usefully override [`prepareRenderer()`](http://www.jarvana.com/jarvana/view/org/swinglabs/swingx-core/1.6.2/swingx-core-1.6.2-javadoc.jar!/org/jdesktop/swingx/JXTable.html#prepareRenderer%28javax.swing.table.TableCellRenderer,%20int,%20int%29) in `JXTable`? – trashgod Dec 19 '12 at 10:56
  • 2
    @trashgod - you can, but shouldn't ;-) JXTable (as all SwingX collection components) has dedicated support for visual cell decorations (aka: highlighters) – kleopatra Dec 19 '12 at 12:53
  • @kleopatra: Thanks for clarifying this; when I saw _color memory_, I immediately thought of you. :-) – trashgod Dec 19 '12 at 14:53
  • It sucks how JXTable doesn't support striping without adding a highlighter (meaning that it will only apply to the table you do it to) when JTable can do it out of the box (meaning that it will apply across all tables.) – Hakanai May 23 '14 at 14:56
  • @Trejkaz How can JTable do it out of the box? The most proposed way I know of is to override `JTable.prepareRenderer()`. – domids Oct 25 '16 at 07:30
  • The `"Table.alternateRowColor"` UIManager property now causes it to render stripes out of the box in maybe all the L&Fs. It's done inside `DefaultTableCellRenderer`, so it could be that all currently-used L&Fs support it. So thinking about it now, some other UIManager property might make JXTable do it for all its tables by default... but I haven't read that deeply into the code and it is probably less hacky to use a highlighter anyway, it's just that using a highlighter means I have to change every table class in my app. – Hakanai Oct 27 '16 at 06:38

1 Answers1

6

To add stripping to your JXTable you need to use HighlighterFactory.
Try:

table.addHighlighter(HighlighterFactory.createSimpleStriping()); 

or:

table.addHighlighter(HighlighterFactory.createAlternateStriping(Color baseBackground, Color alternateBackground)); 

Alternatively, if you want to add multiple highlighters, you can use:

table.setHighlighters(Highlighter... highlighters); 

using always HighlighterFactory to create your highlighters.

Bill Tsagkas
  • 530
  • 1
  • 4
  • 15