JTree的一组节点的背景非不透明(JTree set background of node to

2019-08-08 14:01发布

请看看在SSCCE。 我怎样才能让非选择的树节点的背景是透明的。 目前,非选定节点的背景是白色的。 我的单元格渲染器,但是,如果它漆成非不透明的,如果它没有被选择(选中时,绿色...它做什么)。 最后,我想非选择的节点是没有背景的只是文字,因为这是在SSCCE红色在我的应用程序中的渐变填充的区域。

    import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;

public class SimpleTree extends JFrame
{
    public static void main(final String[] args)
    {
        new SimpleTree();
    }

    public SimpleTree()
    {
        super("Creating a Simple JTree");
        final Container content = this.getContentPane();
        content.setBackground(Color.RED);
        final Object[] hierarchy = { "javax.swing", "javax.swing.border", "javax.swing.colorchooser", "javax.swing.event", "javax.swing.filechooser", new Object[] { "javax.swing.plaf", "javax.swing.plaf.basic", "javax.swing.plaf.metal", "javax.swing.plaf.multi" }, "javax.swing.table",
                        new Object[] { "javax.swing.text", new Object[] { "javax.swing.text.html", "javax.swing.text.html.parser" }, "javax.swing.text.rtf" }, "javax.swing.tree", "javax.swing.undo" };
        final DefaultMutableTreeNode root = this.processHierarchy(hierarchy);
        final JTree tree = new JTree(root);
        tree.setOpaque(false);
        tree.setCellRenderer(new MyCellRenderer());
        final JScrollPane scroller = new JScrollPane(tree);
        scroller.getViewport().setOpaque(false);
        scroller.setOpaque(false);
        content.add(scroller, BorderLayout.CENTER);
        this.setSize(275, 300);
        this.setVisible(true);
    }

    /**
     * Small routine that will make node out of the first entry in the array,
     * then make nodes out of subsequent entries and make them child nodes of
     * the first one. The process is repeated recursively for entries that are
     * arrays.
     */

    private DefaultMutableTreeNode processHierarchy(final Object[] hierarchy)
    {
        final DefaultMutableTreeNode node = new DefaultMutableTreeNode(hierarchy[0]);
        DefaultMutableTreeNode child;
        for (int i = 1; i < hierarchy.length; i++)
        {
            final Object nodeSpecifier = hierarchy[i];
            if (nodeSpecifier instanceof Object[]) // Ie node with children
                child = this.processHierarchy((Object[]) nodeSpecifier);
            else
                child = new DefaultMutableTreeNode(nodeSpecifier); // Ie Leaf
            node.add(child);
        }
        return (node);
    }

    public class MyCellRenderer extends DefaultTreeCellRenderer
    {
        @Override
        public Component getTreeCellRendererComponent(final JTree tree, final Object value, final boolean sel, final boolean expanded, final boolean leaf, final int row, final boolean hasFocus)
        {
            final Component ret = super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);

            final DefaultMutableTreeNode node = ((DefaultMutableTreeNode) (value));

            this.setText(value.toString());
            if (sel)
            {
                this.setOpaque(true);
                this.setBackground(Color.GREEN);

            }
            else
            {
                this.setOpaque(false);
                this.setBackground(null);
            }
            return ret;
        }
    }
}

Answer 1:

你应该重写getBackgroundNonSelectionColorgetBackgroundSelectionColorgetBackgroundDefaultTreeCellRenderer并返回,像这样适当的值:

public class MyCellRenderer extends DefaultTreeCellRenderer {

    @Override
    public Color getBackgroundNonSelectionColor() {
        return (null);
    }

    @Override
    public Color getBackgroundSelectionColor() {
        return Color.GREEN;
    }

    @Override
    public Color getBackground() {
        return (null);
    }

    @Override
    public Component getTreeCellRendererComponent(final JTree tree, final Object value, final boolean sel, final boolean expanded, final boolean leaf, final int row, final boolean hasFocus) {
        final Component ret = super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);

        final DefaultMutableTreeNode node = ((DefaultMutableTreeNode) (value));
        this.setText(value.toString());
        return ret;
    }
}

这将产生:

其他建议:

  • 创建和操作Swing组件事件指派线程 。
  • 不要延长JFrame不必要的,而创建一个实例并使用它。
  • 不叫setSizeJFrame ,而使用正确的LayoutManager和/或覆盖getPreferredSize()并调用pack()JFrame将它设置可见之前,但增加的所有组件后。
  • 记得拨打JFrame#setDefaultCloseOperation与任一DISPOSE_ON_CLOSEEXIT_ON_CLOSEDISPOSE_XXX通常优选使用,除非Timer S作为这将允许main(String[] args)继续其执行桂已被关闭之后)。


Answer 2:

为了避免背景回填,只是把UIManager.put("Tree.rendererFillBackground", false); 之前new SimpleTree(); 或后super("Creating a Simple JTree");



文章来源: JTree set background of node to non-opaque