How to highlight every other row in JTable with swingx 1.6
Asked Answered
D

1

8

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?

Danaus answered 19/12, 2012 at 9:19 Comment(6)
Can you usefully override prepareRenderer() in JXTable?Putsch
@Putsch - you can, but shouldn't ;-) JXTable (as all SwingX collection components) has dedicated support for visual cell decorations (aka: highlighters)Sixtieth
@kleopatra: Thanks for clarifying this; when I saw color memory, I immediately thought of you. :-)Putsch
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.)Bobbibobbie
@Trejkaz How can JTable do it out of the box? The most proposed way I know of is to override JTable.prepareRenderer().Sewing
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.Bobbibobbie
R
7

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.

Referential answered 19/12, 2012 at 12:3 Comment(1)
Brilliant! This is a solution I was looking for! Thank u!Danaus

© 2022 - 2024 — McMap. All rights reserved.