SwingX JXTable: use ColorHighlighter for colored strings based on the "string object",

I am using JXTable and I know how to do it based on DefaultRenderers for JTable, but I want to know how to do it like 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 lines displaying objects of a certain type of a different color.

Looks like I should use ColorHighlighter . But I can’t find examples for this, except for simple markers, such as "the color of every other line" or some such things.

I need the number of rows, since there is no such thing as a “row object” in the JTable / TableModel paradigm, but if I can do this, I can easily check the predicate and return true / false to tell the marker or not.

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

+5
source share
1 answer

I don’t think I understood that. It was just hard to figure out how to use the ComponentAdapter correctly.

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);
+4
source

All Articles