改变树叶子的背景颜色

时间:2012-10-18 07:23:57

标签: swing background-color jtree

想要在一个特定条件下改变树叶的颜色。

if (l_sel_node_object.getIsEnabled().trim().equalsIgnoreCase("N"))
    defaultTreeCellRenderer.setBackgroundSelectionColor(Color.red);

但它正在改变每个选定节点的背景。任何人都可以帮助我。

1 个答案:

答案 0 :(得分:3)

仅在金属外观上测试:

enter image description here

import java.awt.*;
import javax.swing.*;
import javax.swing.tree.*;

public class MainPanel {
  public JPanel makeUI() {
    JTree tree = new JTree();
    tree.setCellRenderer(new RedTreeCellRenderer());
    JPanel p = new JPanel(new BorderLayout(5, 5));
    p.add(new JScrollPane(tree));
    return p;
  }
  public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
      @Override public void run() {
        createAndShowGUI();
      }
    });
  }
  public static void createAndShowGUI() {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    f.getContentPane().add(new MainPanel().makeUI());
    f.setSize(320, 240);
    f.setLocationRelativeTo(null);
    f.setVisible(true);
  }
}
//*
class RedTreeCellRenderer extends DefaultTreeCellRenderer {
  @Override public Component getTreeCellRendererComponent(
        JTree tree, Object value, boolean isSelected,
        boolean expanded, boolean leaf, int row, boolean hasFocus) {
    JComponent c = (JComponent)super.getTreeCellRendererComponent(
        tree, value, isSelected, expanded, leaf, row, hasFocus);
    if(isSelected) {
      c.setForeground(getTextSelectionColor());
      if (leaf && value.toString().equalsIgnoreCase("red")) {
        //<strong>
        c.setOpaque(true);
        //</strong>
        c.setBackground(Color.RED);
      } else {
        c.setOpaque(false);
        c.setBackground(getBackgroundSelectionColor());
      }
    } else {
      c.setOpaque(false);
      c.setForeground(getTextNonSelectionColor());
      c.setBackground(getBackgroundNonSelectionColor());
    }
    return c;
  }
}
/*/
class RedTreeCellRenderer2 extends DefaultTreeCellRenderer {
  @Override public Component getTreeCellRendererComponent(
    JTree tree, Object value, boolean isSelected,
    boolean expanded, boolean leaf, int row, boolean hasFocus) {
    JComponent c = (JComponent)super.getTreeCellRendererComponent(
                     tree, value, isSelected, expanded, leaf, row, hasFocus);
    if(isSelected) {
      if(leaf) {
        setParticularCondition(value.toString());
      }
      c.setForeground(getTextSelectionColor());
      c.setBackground(getBackgroundSelectionColor());
    } else {
      c.setForeground(getTextNonSelectionColor());
      c.setBackground(getBackgroundNonSelectionColor());
    }
    return c;
  }
  boolean particularCondition = false;
  private void setParticularCondition(String str) {
    particularCondition = str.equalsIgnoreCase("red");
  }
  @Override public Color getBackgroundSelectionColor() {
    if(particularCondition) return Color.RED;
    else return super.getBackgroundSelectionColor();
  }
}
//*/