2

我正在使用带有多个 ColorHighlighter 的 JXTreeTable 来更改包含特定值的行的背景颜色。

如果选择了一行,颜色将变为 SelectionBackground。

是否可以在没有全新渲染器的情况下根据谓词或颜色荧光笔更改选择背景颜色?

编辑:如果选择了一行并且有突出显示,则应该是另一种背景颜色,就像没有突出显示的行一样。

SSCCE

package testing;

import java.awt.Color;
import java.awt.Component;

import javax.swing.JFrame;
import javax.swing.JScrollPane;

import org.jdesktop.swingx.JXTreeTable;
import org.jdesktop.swingx.decorator.ColorHighlighter;
import org.jdesktop.swingx.decorator.ComponentAdapter;
import org.jdesktop.swingx.decorator.HighlightPredicate;
import org.jdesktop.swingx.treetable.AbstractMutableTreeTableNode;
import org.jdesktop.swingx.treetable.DefaultTreeTableModel;

public class JXTreeTableDemo extends JFrame {

    public static void main(String[] args) {

        JXTreeTableDemo gui = new JXTreeTableDemo();

        // get root node and 20 child nodes
        ArrayNode root = new ArrayNode(new Object[] { "root", "" });
        for (int i = 0; i < 20; i++) {
            root.add(new ArrayNode(new Object[] { "child", i }));
        }

        // get table and add root node
        JXTreeTable table = new JXTreeTable(new DefaultTreeTableModel(root));
        JScrollPane scrPane = new JScrollPane(table);

        // predicate to highlight all VAL%3==0
        HighlightPredicate predicateMod3 = new HighlightPredicate() {

            @Override
            public boolean isHighlighted(Component renderer,
                    ComponentAdapter adapter) {
                if ((int) adapter.getValue(1) % 3 == 0) {
                    return true;
                }
                return false;
            }

        };

        // predicate to highlight all VAL%4==0
        HighlightPredicate predicateMod4= new HighlightPredicate() {

            @Override
            public boolean isHighlighted(Component renderer,
                    ComponentAdapter adapter) {
                if ((int) adapter.getValue(1) % 4 == 0) {
                    return true;
                }
                return false;
            }

        };

        // initialize highlighter for %3 div 
        ColorHighlighter highlighterMod3 = new ColorHighlighter(predicateMod3,
                Color.BLUE, Color.WHITE);           
        // initialize highlighter for %4 div
        ColorHighlighter highlighterMod4 = new ColorHighlighter(predicateMod4,
                Color.GREEN, Color.WHITE);    
        // add highlighter 
        table.addHighlighter(highlighterMod3);
        table.addHighlighter(highlighterMod4);          
        gui.getContentPane().add(scrPane);
        gui.setVisible(true);
        gui.pack();
    }

    // concrete class of AbstractMutableTreeTableNodes
    static class ArrayNode extends AbstractMutableTreeTableNode {

        public ArrayNode(Object[] data) {
            super(data);
        }

        @Override
        public Object getValueAt(int column) {
            return getUserObject()[column];
        }

        @Override
        public void setValueAt(Object aValue, int column) {
            getUserObject()[column] = aValue;
        }

        @Override
        public int getColumnCount() {
            return 2;
        }

        @Override
        public Object[] getUserObject() {
            return (Object[]) super.getUserObject();
        }

        @Override
        public boolean isEditable(int column) {
            return true;
        }    
    }    
}

到目前为止,mjohannes

4

0 回答 0