JDK-4244496 : Adding node to empty JTree with root node hidden
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.2.2
  • Priority: P4
  • Status: Closed
  • Resolution: Not an Issue
  • OS: generic
  • CPU: generic
  • Submitted: 1999-06-07
  • Updated: 1999-06-21
  • Resolved: 1999-06-21
Description

Name: skT88420			Date: 06/07/99


1) Create a JTree with its root's visibility set to false
2) Set the root node of that tree's model to a node that has no children
3) Insert a node into the model as a child of the root node

=> The child node does not get displayed in the tree because the invisible root node is not set to be expanded.  There does not seem to be an easy way to set the root node to be expandable for this case without the hack indicated within the following code snippet...

import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.DefaultMutableTreeNode;

public class TreeTest extends JFrame {

	public TreeTest() {
		super("Tree Test");
		
		JTree tree = new JTree();
		tree.setRootVisible(false);
		DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
		DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("root");
		//DefaultMutableTreeNode emptyTreeNode = new DefaultMutableTreeNode(); // hack
		//rootNode.add(emptyTreeNode); // hack
		model.setRoot(rootNode);
		//model.removeNodeFromParent(emptyTreeNode); // hack
		model.insertNodeInto(new DefaultMutableTreeNode("child node"), rootNode, 0);
		setContentPane(tree);
	}
	
	public static void main(String args[]) {
		TreeTest test = new TreeTest();
		test.setSize(new Dimension(300, 300));
		test.addWindowListener(
				new WindowAdapter() {
			    	public void windowClosing(WindowEvent e) {System.exit(0);}
				}
			);
		test.setVisible(true);
	}
}
(Review ID: 84003) 
======================================================================

Comments
WORK AROUND Name: skT88420 Date: 06/07/99 see lines of code commented with // hack ======================================================================
11-06-2004

EVALUATION The child is not displayed because the root is not expanded. To expand it, do a: tree.expandPath(new TreePath(treeModel.getRoot())); scott.violet@eng 1999-06-21
21-06-1999

PUBLIC COMMENTS The child is not displayed because the root is not expanded. To expand it, do a: tree.expandPath(new TreePath(treeModel.getRoot())); scott.violet@eng 1999-06-21
21-06-1999