JXTreeTable combined with TreeCellRender and selectedBackground with ColorHighlighter

I am trying to set a custom selectionBackground for rows in a JXTreeTable. This works if I don't customize my own TreeCellRenderer. If I set it additionally, like in my example, fetchingBackund of the text node is the default. Any ideas on how to get the background of a nodetext to a custom one?

import java.awt.Color;

import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.UIManager;

import org.jdesktop.swingx.JXTreeTable;
import org.jdesktop.swingx.decorator.ColorHighlighter;
import org.jdesktop.swingx.decorator.HighlightPredicate;
import org.jdesktop.swingx.renderer.DefaultTreeRenderer;
import org.jdesktop.swingx.renderer.IconValue;
import org.jdesktop.swingx.treetable.DefaultMutableTreeTableNode;
import org.jdesktop.swingx.treetable.DefaultTreeTableModel;

public class TestHighlighter {

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JScrollPane scrollPane = new JScrollPane();
        frame.setContentPane(scrollPane);

        JXTreeTable treeTable = new JXTreeTable(new DefaultTreeTableModel(new DefaultMutableTreeTableNode("Test")));
        treeTable.setRootVisible(true);
        scrollPane.setViewportView(treeTable);

        DefaultTreeRenderer treeCellRenderer = new DefaultTreeRenderer(new IconValue() {
            @Override
            public Icon getIcon(Object value) {
                return UIManager.getIcon("FileView.directoryIcon");
            }
        });
        //Comment out next line and background is like set in Highlighter
        treeTable.setTreeCellRenderer(treeCellRenderer);

        treeTable.addHighlighter(new ColorHighlighter(HighlightPredicate.ALWAYS, null, null, Color.RED, null));

        frame.pack();
        frame.setVisible(true);
    }
}

      

I also tried using IconHighlighter (to avoid using a custom TreeCellRenderer) to change the node icon, but the icon hasn't changed.

treeTable.addHighlighter(new IconHighlighter(HighlightPredicate.ALWAYS, UIManager.getIcon("FileView.directoryIcon")));

      

+3


source to share


1 answer


[...] the problem is that I want to have different selectionBackgrounds depending on the node. [...] I thought there is a possibly more elegant way using the swingx renderer.

You can use the SwingX renderer (aka :) DefaultTreeRenderer

and override getCellRendererComponent (...) to set the background color depending on the node as you wish. For example:



IconValue iconValue = new IconValue() {
    @Override
    public Icon getIcon(Object value) {
        return UIManager.getIcon("FileView.directoryIcon");
    }
};

DefaultTreeRenderer treeCellRenderer = new DefaultTreeRenderer(iconValue) {
    @Override
    public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
        Component c = super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
        if (selected && leaf) {
            c.setBackground(Color.RED);
        } else {
            setBackground(tree.getBackground());
        }
        return c;
    }
};

      

In this snippet, if a leaf (not root or parent) node is selected then the background color of the label will be red. Otherwise, the background color of the label will be the default.

+2


source







All Articles