SwingX JXTable: use ColorHighlighter to color rows

2019-02-17 15:37发布

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?

1条回答
Bombasti
2楼-- · 2019-02-17 16:34

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);
查看更多
登录 后发表回答