This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes How to set multiple icons in a TreeCell? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to set multiple icons in a TreeCell?" Watch "How to set multiple icons in a TreeCell?" New topic
Author

How to set multiple icons in a TreeCell?

S Dan
Greenhorn

Joined: Apr 05, 2005
Posts: 25
I'm trying to set multiple icons in my tree cells. I'm using my custom tree cell renderer that extends the DefaultTreeCell Renderer. In my code, I'm setting the
icons for the cell based on the Node type which seem to be working fine. Now I'd like to add another icon to some of the special nodes.
Now this is what I've been trying (sort of pseudocode)..

public class MyTreeCellRenderer extends DefaultTreeCellRenderer {

public Component getTreeCellRendererComponent(
JTree tree,
Object value,
boolean selected,
boolean expanded,
boolean leaf,
int row,
boolean hasFocus) {
final Component c = super.getTreeCellRendererComponent(
tree, cellText, selected,
expanded, leaf, row,
hasFocus);

MyTreeNode node = (MyTreeNode)value;
if (node is of special type){
c.setIcon(image);
}
if (node is of very special type){
FlowLayout layout = new FlowLayout();
layout.setHgap(20);
((JComponent)c).setLayout(layout);
JLabel label = new JLabel(new ImageIcon(AnotherImage));
c.add(label);
}
return c;
}
}

But when I try to add AnotherImage to the very special node, it applies to all the nodes and I see two images in both the nodes (as if it is completely ignoring the if stmt). Any idea whats going on?
Thanks.
-Dan

[ April 21, 2005: Message edited by: S Dan ]

[ April 21, 2005: Message edited by: S Dan ]
[ April 21, 2005: Message edited by: S Dan ]
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to set multiple icons in a TreeCell?
 
Similar Threads
Turning JTree into a layout manager, resizing nodes
JCheckBox in Jtree Problem
help me I can't work the tree well
Problems with JTree and CheckBoxes
how can I change this tree with different Icons for Different nodes at Run Time