Creating Dynamic JTrees (Root Node Visibility Management)

I have a question on how to dynamically generate JTrees. Is there a way to install Root Node invisible without making its children invisible too? I tried to do the following, but it shows all nodes as invisible. Keep in mind that I want to add and remove Root Node children at any given time. I added comments so you can keep track of what I intend to do. Let me know if they do what I don't need, as I am new to JTrees and don't know the conventions. I would also like to be able to select multiple children for the listener.

    DefaultMutableTreeNode rootNode;
    rootNode = new DefaultMutableTreeNode(); //I want this invisible.

    DefaultTreeModel treeModel = new DefaultTreeModel(rootNode);
    JTree tree = new JTree(treeModel);

    treeModel.addTreeModelListener(this);
    tree.setRootVisible(false); // Sets everything invisible
    tree.setEditable(true); //makes tree dynamic
    tree.setShowsRootHandles(true); //supposedly allows you to see the children of the nodes.

    tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); 
    //I would like the line above to be multi-select; however, this doesn't seem to be an option.

    DefaultMutableTreeNode table = new DefaultMutableTreeNode( "table1");
    rootNode.add(book);

    DefaultMutableTreeNode value = new DefaultMutableTreeNode( "value");
    table.add(value);

In the above example. Nothing is displayed, and when I delete "tree.setRootVisible (false)" everything is visible, including node.

+3
4

. TreeDemo Swing . , , .

+1

, . , node, :

yourTree.expandPath(new TreePath(root.getPath()))
+6

, TreeDemo , . ( ), TreeModel. , , , . , , , -, node ", :

, DefaultMutableTreeNode node, DefaultTreeModel . , , , .

-

+2

, java , .

setRootVisible (false) , setShowsRootHandles (true), :

tree.setRootVisible(false);
tree.setShowsRootHandles(true)

, ! https://docs.oracle.com/javase/tutorial/uiswing/components/tree.html#display

0

Source: https://habr.com/ru/post/1725886/


All Articles