ColorHighlighter on JXTreeTable - Change selection

2019-02-25 22:03发布

问题:

I'm using an JXTreeTable with several ColorHighlighters to change the background color of the rows that include a specific value.

If a row is selected, the color will change to SelectionBackground.

Is it possible to change the selection background color based on the predicates or color highlighters without an complete new renderer?

EDIT: If a row is selected and has an highlighting, ther should be another background color as on a row where is no highlighting.

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

So far, mjohannes