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

1

7

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?

Guildsman answered 20/3, 2012 at 17:17 Comment(0)
G
4

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);
Guildsman answered 20/3, 2012 at 19:22 Comment(7)
glad you found a solution - just beware: the adapter.row is in view-coordinates while your itemList (most probably?) is in model-coordinates, so the test will return incorrect results if the table is sorted/filtered. To fix, call adapter.convertRowIndexToModelPlaza
ok thanks -- I'm disabling JXTable's sorting on purpose to use GlazedLists sorting facilities, so my indices are coherent with the final displayed rows. thanks though!Guildsman
p.s. what happened to Highlighter pipelines? are they gone? I'm trying to find general info on JXTable highlighters and it's a random collection of out of date stuff.Guildsman
yeah, the pipelines are gone (no need to keep them after targeting jdk6). Documentation is at shambles, sorry for that - the only way to learn is by-example: demos, code in the test packages, some sections in the incubator and maybe the wiki rendering design notes (many broken links due to project migration ... arrgghhh) Feel invited to ask if you run into problems :-)Plaza
@JasonS I'm having similar requirement. But I'm new with SwingX and I tried copy-pasting your code, but Item, adapter.row etc. doesn't seem to resolve. Do you have a complete working example ?Sawyor
@Plaza my jxtreetable is sorted and I see it'll create problem. But I'm unable to get how to solve it because the method you specified doesn't exist either. Do you have a complete working example?Sawyor
@XCoder don't know what you mean by sorted (sorting/filtering is not supported by treetable). Anyway, nothing new to my last comment which enumerated some resources which contain full-fledged examples. And please post your own question, preferably with a SSCCE :-)Plaza

© 2022 - 2024 — McMap. All rights reserved.