Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to highlight every other row in JTable with swingx 1.6

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?

like image 467
Dakarth Avatar asked Dec 19 '12 09:12

Dakarth


People also ask

How can I tell if a row is selected in JTable?

JTable has a getSelectionModel() method which will give you a ListSelectionModel object. It tells you what rows are selected.

How do you clear a JTable row?

If using the DefaultTableModel , just set the row count to zero. This will delete the rows and fire the TableModelEvent to update the GUI. JTable table; … DefaultTableModel model = (DefaultTableModel) table.


1 Answers

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.

like image 188
Bill Tsagkas Avatar answered Sep 27 '22 22:09

Bill Tsagkas