Article View: borland.public.jbuilder.java.swing
Article #10918Re: Icons for each node in a Tree
From: Tad Frysinger TeamB Tad Frysinge...
Date: Mon, 26 Jul 1999 00:00
Date: Mon, 26 Jul 1999 00:00
70 lines
2708 bytes
2708 bytes
Make sure your images are actually getting loaded ok. Paths can be a
real pain.
Then, if all your icons are the same, this is really easy.
DefaultTreeCellRenderer is actually based on a JLabel, so simply
substitute the icon with your image.
If different nodes have different icons, then this is a bit trickier.
Here is my renderer, I've used a JGL Pair object to wrap the icon and the
text:
class PairTreeCellRenderer extends DefaultTreeCellRenderer
{
public void PairTreeCellRenderer()
{
setLeafIcon( null );
setClosedIcon( null );
setOpenIcon( null );
}
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 );
pair = (Pair) ((DefaultMutableTreeNode)value).getUserObject();
setText( pair.second.toString() );
setIcon( (Icon)pair.first );
selected = sel;
return this;
}
public String getToolTipText()
{
return "";
}
private Pair pair;
}
Then, here is how I use this:
ImageIcon imageDatabase = new ImageIcon( IMAGE_PATH + "database.gif" );
DefaultMutableTreeNode tempNode = null;
rootNode = new DefaultMutableTreeNode( new Pair( null, "Objects") );
treeModel = new DefaultTreeModel(rootNode);
conMetaTree.setModel( treeModel );
conMetaTree.setCellRenderer( new PairTreeCellRenderer());
// load the nodes
tempNode = new DefaultMutableTreeNode( new Pair( imageDatabase, s));
treeModel.insertNodeInto( tempNode,
rootNode,
rootNode.getChildCount() );
etc.
Tad Frysinger (TeamB)
Technical Details
Message-ID:
<MPG.1206849d51cfc4fd989683@forums.borland.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<379871C9.588B936C@procsys.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 111d45,240329fa40c08b00 X-Google-Attributes: gid111d45,public From: Tad Frysinger (TeamB) (Tad Frysinger) Subject: Re: Icons for each node in a Tree Date: 1999/07/26 Message-ID: <MPG.1206849d51cfc4fd989683@forums.borland.com> X-Deja-AN: 505533076 References: <379871C9.588B936C@procsys.com> Organization: RTOne Newsgroups: borland.public.jbuilder.java.swing