JTree - 如何将背景设置为节点
JTree - how to setBackground to node
我阅读了很多教程,但对我没有帮助。我有 jTree 可以工作并且 setForeground 工作得很好但是当我想 setBackground 所以 jTree 没有变化。你能帮我看看应该怎么写吗。谢谢
import java.awt.Color;
import java.awt.Component;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
public class UrlNodeRenderer extends DefaultTreeCellRenderer {
public static Icon icon = null;
public UrlNodeRenderer() {
icon = new ImageIcon(getClass().getResource("icon.png"));
}
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
setOpenIcon(icon);
setClosedIcon(icon);
setLeafIcon(icon);
UrlTreeNode node = (UrlTreeNode) (((DefaultMutableTreeNode) value).getUserObject());
if(node.isContainsPhrase()) {
setForeground(Color.BLUE);
setBackground(Color.PINK); // doesn't works
}
return this;
}
}
您需要在 TreeCellRenderer
上使用 setOpaque(true);
来更改背景颜色。您可以将其添加到构造函数中:
public UrlNodeRenderer() {
icon = new ImageIcon(getClass().getResource("icon.png"));
setOpaque(true);
}
编辑:
您需要在 false 和 true 情况下更改背景颜色,如下所示:
private class Renderer extends DefaultTreeCellRenderer{
public Renderer() {
setOpaque(true);
}
@Override
public Component getTreeCellRendererComponent(JTree tree, Object value,
boolean selected, boolean expanded, boolean leaf, int row,
boolean hasFocus) {
super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
setBackground(selected ? Color.LIGHT_GRAY :
(leaf ? Color.GRAY : tree.getBackground()));
return this;
}
}
你的情况:
if(node.isContainsPhrase()) {
setForeground(Color.BLUE);
setBackground(Color.PINK); // doesn't works
}
您为所有节点设置了颜色,因为您从未为错误的情况设置颜色。
我阅读了很多教程,但对我没有帮助。我有 jTree 可以工作并且 setForeground 工作得很好但是当我想 setBackground 所以 jTree 没有变化。你能帮我看看应该怎么写吗。谢谢
import java.awt.Color;
import java.awt.Component;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
public class UrlNodeRenderer extends DefaultTreeCellRenderer {
public static Icon icon = null;
public UrlNodeRenderer() {
icon = new ImageIcon(getClass().getResource("icon.png"));
}
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
setOpenIcon(icon);
setClosedIcon(icon);
setLeafIcon(icon);
UrlTreeNode node = (UrlTreeNode) (((DefaultMutableTreeNode) value).getUserObject());
if(node.isContainsPhrase()) {
setForeground(Color.BLUE);
setBackground(Color.PINK); // doesn't works
}
return this;
}
}
您需要在 TreeCellRenderer
上使用 setOpaque(true);
来更改背景颜色。您可以将其添加到构造函数中:
public UrlNodeRenderer() {
icon = new ImageIcon(getClass().getResource("icon.png"));
setOpaque(true);
}
编辑: 您需要在 false 和 true 情况下更改背景颜色,如下所示:
private class Renderer extends DefaultTreeCellRenderer{
public Renderer() {
setOpaque(true);
}
@Override
public Component getTreeCellRendererComponent(JTree tree, Object value,
boolean selected, boolean expanded, boolean leaf, int row,
boolean hasFocus) {
super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
setBackground(selected ? Color.LIGHT_GRAY :
(leaf ? Color.GRAY : tree.getBackground()));
return this;
}
}
你的情况:
if(node.isContainsPhrase()) {
setForeground(Color.BLUE);
setBackground(Color.PINK); // doesn't works
}
您为所有节点设置了颜色,因为您从未为错误的情况设置颜色。