Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwingX JXTable: use ColorHighlighter to color rows based on a "row object"

I'm using JXTable and I know how to do this based on DefaultRenderers for JTable, but I want to know how to do it in a way that's JXTable-friendly based on HighlighterPipeline.

I have a list of objects displayed in a table, and each row represents one object. I would like to color the rows displaying objects of a certain type a different color.

It looks like I should be using ColorHighlighter. But I can't find examples for this, other than the simple highlighters like "color every other row" or some such thing.

I need the row number since there's no such thing as a "row object" in the JTable/TableModel paradigm, but if I can do that, I can easily test a predicate and return true/false to tell the highlighter to kick in or not.

Can someone help me figure out the right direction to get this to work?

like image 743
Jason S Avatar asked Mar 20 '12 17:03

Jason S


1 Answers

never mind, I figured it out. It was just hard to figure out the way to use ComponentAdapter propertly.

JXTable table = ...
final List<Item> itemList = ...

final HighlightPredicate myPredicate = new HighlightPredicate() {
      @Override 
      public boolean isHighlighted(
            Component renderer, 
            ComponentAdapter adapter) {

            Item item = itemList.get(adapter.row);
            return testItem(item);
      }

      public boolean testItem(Item item) { ... }
}

ColorHighlighter highlighter = new ColorHighlighter(
      myPredicate,
      Color.RED,   // background color
      null);       // no change in foreground color

table.addHighlighter(highlighter);
like image 56
Jason S Avatar answered Nov 01 '22 06:11

Jason S