Article View: borland.public.jbuilder.java.swing
Article #11106Re: tree from dataset?
From: Tad Frysinger
Date: Wed, 07 Jul 1999 00:00
Date: Wed, 07 Jul 1999 00:00
73 lines
3011 bytes
3011 bytes
In article <37837FF6.654DDC32@sema4.com>, ahumphreys@sema4.com says...
> What is the best way to populate a java tree from a dataset (or just on
> the fly)? I don't know how many nodes we're going to have, so I can't
> really set up the appropriate number of GraphLocations in the code -- it
> needs to be done dynamically. Thanks. -alex
>
>
Loop through the dataset, grab the info you need and build the tree.
Here is a fragment where I am adding database metadata (column
information) to a tree that describes a database table.
while (rsColumnList.next())
{
// Now add a description for this particular column
columnDescNode = new DefaultMutableTreeNode(
new Pair( imageCol,
rsColumnList.getString( "COLUMN_NAME" )));
treeModel.insertNodeInto( columnDescNode,
columnsNode,
columnsNode.getChildCount() );
}
The DefaultMutableTree node takes an object that it stores in its
'userObject' instance variable. This is usually a string, but in my
example I am using a JGL Pair object. This means I also need to have a
renderer assigned to my tree that knows how to get into that Pair()
object and update a renderer component appropriately:
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;
}
Tad Frysinger (TeamB)
Technical Details
Message-ID:
<MPG.11ed7e74854d8b90989a42@forums.borland.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<37837FF6.654DDC32@sema4.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 111d45,1bbafc452ff20417 X-Google-Attributes: gid111d45,public From: tfrysinger@earthlink.net (Tad Frysinger) Subject: Re: tree from dataset? Date: 1999/07/07 Message-ID: <MPG.11ed7e74854d8b90989a42@forums.borland.com> X-Deja-AN: 498316255 References: <37837FF6.654DDC32@sema4.com> Organization: Freezone Enterprises Newsgroups: borland.public.jbuilder.java.swing