SwingX JXTable:使用ColorHighlighter根据“行对象”对行进行着色。 [英] SwingX JXTable: use ColorHighlighter to color rows based on a "row object"

查看:474
本文介绍了SwingX JXTable:使用ColorHighlighter根据“行对象”对行进行着色。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JXTable,我知道如何基于JTable的DefaultRenderers做到这一点,但我想知道如何以基于HighlighterPipeline的JXTable友好的方式做到这一点。

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.

看起来我应该使用 ColorHighlighter 。但我找不到这方面的例子,除了简单的荧光笔,比如每隔一行颜色或者其他一些东西。

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.

我需要行号,因为没有像JTable / TableModel范例中的行对象这样的东西,但是如果我能做到这一点,我可以轻松地测试一个谓词并返回true / false来告诉荧光笔是否开启。

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?

推荐答案

没关系, 我想到了。很难找出使用ComponentAdapter的方法。

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

这篇关于SwingX JXTable:使用ColorHighlighter根据“行对象”对行进行着色。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆