How to make JTree transparent?
import javax.swing.*;
import javax.swing.tree.*;
import java.awt.*;
publicclass TreeTestFrameextends JFrame
{
public TreeTestFrame()
{
DefaultMutableTreeNode root=new DefaultMutableTreeNode("root");
DefaultMutableTreeNode nodeA=new DefaultMutableTreeNode("A");
DefaultMutableTreeNode nodeB=new DefaultMutableTreeNode("B");
root.add(nodeA);
root.add(nodeB);
DefaultTreeModel model=new DefaultTreeModel(root);
JTree tree=new JTree(model);
tree.setOpaque(false);
tree.setCellRenderer(new TestTreeCellRenderer());
getContentPane().add(tree);
}
publicstaticvoid main(String[] args)
{
Window w=new TreeTestFrame();
w.pack();
w.setVisible(true);
}
}
class TestTreeCellRendererextends DefaultTreeCellRenderer
{
public Component getTreeCellRendererComponent(JTree tree, Object value,boolean sel,boolean expanded,boolean leaf,int row,boolean hasFocus)
{
JLabel label=(JLabel)super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
label.setForeground(Color.GREEN);
label.setBackground(Color.RED);
setOpaque(false);
return label;
}
}
The tree nodes are not transparent. The foreground has become green, but the background is still white after invoking label.setBackground(Color.RED). It seems JTree has constant white background.
Anyone can help? Thanks in advance.
Ling

