-
Bug
-
Resolution: Duplicate
-
P4
-
None
-
1.2.1
-
x86
-
windows_95
Name: vi73552 Date: 05/10/99
I have a LARGE tree of data, which is *partially* reflected by a building a tree of (a subclass of) DefaultMutableTreeNodes. Data for a node is dynamically gathered and loaded only if it's visible in the tree, and once collapsed, it's discarded again, via TreeWillExpandListner.treeWillExpand() and
treeWillCollapse().
I want expand/collapse icons for nodes based on whether the actual data has child entities, not on the actual shape of the DefaultMutableTreeNode tree at a particular instant.
Using asksAllowsChildren==true, and setting allowsChildren appropriately for each DefaultMutableTreeNode ought to give the control I need over expand/collapse icons.
But:
a) the root node never has an expand icon, only a contract icon if it's expanded.
b) each parent node (ie allowsChildren==true) only has an expand icon when it first becomes visable as it's parent is expand. After collapsing no expand icon is ever shown again if ever it renenters the expanded state. The collapse icon appears as expected.
java version "1.2.1"
Classic VM (build JDK-1.2.1-A, native threads)
JAVA.EXE full version "JDK-1.2.1-A"
Perhaps DynamicUtilTreeNode is the answer, but the documentation doesn't say if rehidden nodes are discarded. In fact there seems to be very little documentation on this class generally.
Below is a simpler program I hacked up with the same problem:
============================================================
import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.event.*;
import java.awt.event.*;
class TreeViewer extends JFrame implements TreeWillExpandListener
{
DefaultMutableTreeNode root;
JTree tree;
DefaultTreeModel treeModel;
TreeViewer()
{
root = new DefaultMutableTreeNode("Root", true); //root node that allows children
treeModel = new DefaultTreeModel(root,true);
tree = new JTree(treeModel);
tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
tree.setShowsRootHandles(true); //put expand/shrink handle on root too
tree.putClientProperty("JTree.lineStyle", "Angled"); //Draw in branches
tree.addTreeWillExpandListener(this);
this.getContentPane().add(tree);
}
public void treeWillExpand(TreeExpansionEvent e)
{
DefaultMutableTreeNode selectedNode;
DefaultMutableTreeNode child;
selectedNode= (DefaultMutableTreeNode)e.getPath().getLastPathComponent();
if(selectedNode.getUserObject().equals("Root")) //then add the child
{
child = new DefaultMutableTreeNode("a (childless)", false);
selectedNode.add(child);
child = new DefaultMutableTreeNode("b (has children)", true);
selectedNode.add(child);
}
else if(selectedNode.getUserObject().equals("b (has children)")) //then add the child
{
child = new DefaultMutableTreeNode("c (childless)", false);
selectedNode.add(child);
}
treeModel.reload();
}
public void treeWillCollapse(TreeExpansionEvent e)
{
DefaultMutableTreeNode selectedNode;
selectedNode= (DefaultMutableTreeNode)e.getPath().getLastPathComponent();
selectedNode.removeAllChildren();
treeModel.reload();
}
public static void main(String args[])
{
TreeViewer tv = new TreeViewer();
tv.setSize(500,500);
tv.setVisible(true);
tv.addWindowListener
(
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
}
}
============================================================
(Review ID: 57296)
======================================================================
- duplicates
-
JDK-4237066 Trees: Expansion icons disappear after collapsing with dynamic tree
-
- Closed
-