覆盖选择颜色 - Jtree

时间:2016-12-03 10:11:16

标签: java swing colors jtree

DefaultTreeCellRenderer中有一种名为setBackgroundSelectionColor()的方法,可让您在JTree上选择项目时更改选择颜色。当我输入DefaultTreeCellRenderer的源代码时,选择颜色在paint() getBackgroundSelectionColor()中设置。

下面的代码中,我试图通过简单地覆盖paint方法来更改选择颜色。问题是即使在paint方法中手动设置,颜色也保持不变。即使setBackgroundSelectionColor()中不再使用getBackgroundSelectionColor(),方法paint仍然有效。

为什么会这样,以及如何正确覆盖颜色?
不使用setBackgroundSelectionColor()

import java.awt.*;

import javax.swing.*;
import javax.swing.tree.*;

@SuppressWarnings("serial")
public class DirectoryExplorer extends JFrame {
    private DirectoryExplorer() {
        super("Directory Explorer");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new GridLayout(1, 1));
        createPanel();
        setSize(800,600);
        setVisible(true);
    }

    private void createPanel() {
        JPanel panel = new JPanel(new GridLayout(1, 1));
        JTree tree = new JTree();

        paintRenderer pR = new paintRenderer();
        tree.setCellRenderer(pR);

        //pR.setBackgroundSelectionColor(Color.RED); //Why does this work when changing value in paint doesn't

        panel.add(new JScrollPane(tree));
        getContentPane().add(panel);
    }

    public static void main(String[] args) {
        new DirectoryExplorer();
    }

    private class paintRenderer extends DefaultTreeCellRenderer {
        @Override
        public void paint(Graphics g) {
            Color bColor;

            if (selected) {
                bColor = Color.RED;//= getBackgroundSelectionColor();
            } else {
                bColor = getBackgroundNonSelectionColor();
                if (bColor == null) {
                    bColor = getBackground();
                }
            }

            //super.paint(g); //Paints the correct colors but no text

            int imageOffset = -1;
            if (bColor != null) {
                imageOffset = getLabelStart();
                g.setColor(bColor);
                if(getComponentOrientation().isLeftToRight()) {
                    g.fillRect(imageOffset, 0, getWidth() - imageOffset, getHeight());
                } else {
                    g.fillRect(0, 0, getWidth() - imageOffset, getHeight());
                }
            }

            if (hasFocus) {
                if (imageOffset == -1) {
                    imageOffset = getLabelStart();
                }

                if(getComponentOrientation().isLeftToRight()) {
                    paintFocus(g, imageOffset, 0, getWidth() - imageOffset, getHeight(), bColor);
                } else {
                    paintFocus(g, 0, 0, getWidth() - imageOffset, getHeight(), bColor);
                }
            }
            super.paint(g); //Paints text but wrong colors
        }

        private void paintFocus(Graphics g, int x, int y, int w, int h, Color notColor) {
            Color bsColor = Color.RED;//= getBorderSelectionColor();

            if (bsColor != null && selected) {
                g.setColor(bsColor);
                g.drawRect(x, y, w - 1, h - 1);
            }
        }

        private int getLabelStart() {
            Icon currentI = getIcon();
            if(currentI != null && getText() != null) {
                return currentI.getIconWidth() + Math.max(0, getIconTextGap() - 1);
            }
            return 0;
        }
    }
}

修改
它目前的样子

enter image description here

1 个答案:

答案 0 :(得分:0)

问题是您要覆盖paint(...)而不是paintComponent(...)

正如answer中所述,您不应该覆盖paint以寻找不是顶级容器的东西,而应该覆盖paintComponent

将代码更改为类似的内容。

@Override
public void paintComponent(Graphics g) {
...
super.paintComponent(g);
}

例如

import java.awt.*;

import javax.swing.*;
import javax.swing.tree.*;

@SuppressWarnings("serial")
public class DirectoryExplorer extends JFrame {
    private DirectoryExplorer() {
        super("Directory Explorer");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new GridLayout(1, 1));
        createPanel();
        setSize(800,600);
        setVisible(true);
    }

    private void createPanel() {
        JPanel panel = new JPanel(new GridLayout(1, 1));
        JTree tree = new JTree();

        paintRenderer pR = new paintRenderer();
        tree.setCellRenderer(pR);

        panel.add(new JScrollPane(tree));
        getContentPane().add(panel);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> new DirectoryExplorer());
    }

    private class paintRenderer extends DefaultTreeCellRenderer {
        @Override
        public void paintComponent(Graphics g) {
            Color bColor;

            if (selected) {
                bColor = Color.RED;
            } else {
                bColor = getBackgroundNonSelectionColor();
                if (bColor == null) {
                    bColor = getBackground();
                }
            }

            int imageOffset = -1;
            if (bColor != null) {
                imageOffset = getLabelStart();
                g.setColor(bColor);
                if(getComponentOrientation().isLeftToRight()) {
                    g.fillRect(imageOffset, 0, getWidth() - imageOffset, getHeight());
                } else {
                    g.fillRect(0, 0, getWidth() - imageOffset, getHeight());
                }
            }

            if (hasFocus) {
                if (imageOffset == -1) {
                    imageOffset = getLabelStart();
                }

                if(getComponentOrientation().isLeftToRight()) {
                    paintFocus(g, imageOffset, 0, getWidth() - imageOffset, getHeight());
                } else {
                    paintFocus(g, 0, 0, getWidth() - imageOffset, getHeight());
                }
            }
            super.paintComponent(g);
        }

        private void paintFocus(Graphics g, int x, int y, int w, int h) {
            Color bsColor = Color.RED;

            if (bsColor != null && selected) {
                g.setColor(bsColor);
                g.drawRect(x, y, w - 1, h - 1);
            }
        }

        private int getLabelStart() {
            Icon currentI = getIcon();
            if(currentI != null && getText() != null) {
                return currentI.getIconWidth() + Math.max(0, getIconTextGap() - 1);
            }
            return 0;
        }
    }
}