File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
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
 
 
subject: How to set multiple icons in a TreeCell?