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

自作多情 提交于 2019-12-30 09:34:21

问题


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:


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);


来源:https://stackoverflow.com/questions/9791719/swingx-jxtable-use-colorhighlighter-to-color-rows-based-on-a-row-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!