6

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?

Kevin Reid
  • 37,492
  • 13
  • 80
  • 108
Jason S
  • 184,598
  • 164
  • 608
  • 970

1 Answers1

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);
Paco Abato
  • 3,920
  • 4
  • 31
  • 54
Jason S
  • 184,598
  • 164
  • 608
  • 970
  • 1
    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.convertRowIndexToModel – kleopatra Mar 21 '12 at 10:02
  • 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! – Jason S Mar 21 '12 at 12:46
  • 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. – Jason S Mar 21 '12 at 12:48
  • 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 :-) – kleopatra Mar 21 '12 at 13:11
  • @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 ? – coding_idiot Mar 16 '13 at 10:55
  • @kleopatra 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? – coding_idiot Mar 16 '13 at 10:57
  • @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 :-) – kleopatra Mar 16 '13 at 11:26