Hi All
I have here a potential bug .. I hope that it is not ...
I am using the jTree with DefaultTreeModel ; adding nodes through a
loop.
I got a snippet of code from CoreJava 2 Book to make a node visible ..
then I call this every time I add a node just to expand its parent and
show it ..
The strange thing is that nothing happen !! Try the code your self and
tell me what is wrong here ...
---------------------------------
package mtse.Test;
import javax.swing.JFrame;
import java.awt.Dimension;
import javax.swing.JTree;
import javax.swing.tree.*;
import java.awt.Rectangle;
import java.awt.BorderLayout;
public class TreeFrame extends JFrame
{
private JTree jTree1 = new JTree();
private BorderLayout borderLayout1 = new BorderLayout();
private DefaultTreeModel model=null;
public TreeFrame()
{
try
{
jbInit();
}
catch(Exception e)
{
e.printStackTrace();
}
DefaultMutableTreeNode root=new DefaultMutableTreeNode("Root");
model=new DefaultTreeModel(root);
jTree1.setModel(model);
DefaultMutableTreeNode f1=new DefaultMutableTreeNode("F 1");
DefaultMutableTreeNode f2=new DefaultMutableTreeNode("F 2");
addLinks(f1);
addLinks(f2);
root.add(f1);
root.add(f2);
/*
//
TreeNode[] nodes = model.getPathToRoot(o5);
TreePath path = new TreePath(nodes);
System.out.println("Path\r\n" + path);
jTree1.makeVisible(path);
//
*/
}
private void jbInit() throws Exception
{
this.getContentPane().setLayout(borderLayout1);
this.setSize(new Dimension(400, 300));
this.getContentPane().add(jTree1, BorderLayout.CENTER);
}
public static void main(String[] s)
{
TreeFrame f=new TreeFrame();
f.show();
}
private void addLinks(DefaultMutableTreeNode hub){
for(int i=0;i<3;i++){
DefaultMutableTreeNode n=new DefaultMutableTreeNode(
String.valueOf(i) );
hub.add(n);
TreeNode[] nodes = model.getPathToRoot(n);
TreePath path = new TreePath(nodes);
System.out.println("Path\r\n" + path);
jTree1.makeVisible(path);
}
}
private void showNode(DefaultMutableTreeNode n){
TreeNode[] nodes = model.getPathToRoot(n);
TreePath path = new TreePath(nodes);
System.out.println("Path\r\n" + path);
jTree1.makeVisible(path);
}
}
andrey.makarewitsch - 22 Dec 2004 19:09 GMT
hi,
> I have here a potential bug .. I hope that it is not ...
well, just another "feature". ;-)
> addLinks(f1);
> addLinks(f2);
>
> root.add(f1);
> root.add(f2);
^^^
"add" collapse your node (I don't know if this effect a bug or some kind
of feature). try:
root.add(f1);
root.add(f2);
addLinks(f1);
addLinks(f2);
and:
> /* ... skipped ... */
> DefaultMutableTreeNode n=new DefaultMutableTreeNode(
> String.valueOf(i) );
> hub.add(n);
((javax.swing.tree.DefaultTreeModel)jTree1.getModel()).nodeStructureChanged(hub);
> TreeNode[] nodes = model.getPathToRoot(n);
> TreePath path = new TreePath(nodes);
> jTree1.makeVisible(path);
> /* ... skipped ... */
Christian Kaufhold - 31 Dec 2004 19:19 GMT
> I am using the jTree with DefaultTreeModel ; adding nodes through a
> loop.
You are not supposed to use the TreeNode modification methods if the
nodes are part of a DefaultTreeModel. Use DefaultTreeModel.insert-
NodeInto.
Christian