开发者

Letting User add Nodes to a JTree, nodes do not appear if parent has EVER been expanded

开发者 https://www.devze.com 2023-01-04 10:15 出处:网络
I have a problem with inserting nodes in a JTree.Basically the user right clicks on a parent node, bringing up a list of the possible children to insert.They select one it is inserted as a child of th

I have a problem with inserting nodes in a JTree. Basically the user right clicks on a parent node, bringing up a list of the possible children to insert. They select one it is inserted as a child of the selected node. This all works great when the parent node has never been expanded. However, if the parent node of the node the user is trying to insert has EVER been expanded, the UI doesn't seem to update accordingly.

Here are the relevant lines of code: Creating the JTree: this.tree = new JTree(ctlr.getRulesTableTreeModel().getModel()); The getModel() method returns a DefaultTreeModel models the original tree hierarchy.

Inserting a Node: this.treeModel.insertNodeInto(child, parent, index);

Sorry for not giving more code, bu开发者_运维技巧t this project is like 3000 lines long, I'm trying to pick out the relevant lines.


you need to update the model:

per the default tree model api:

http://java.sun.com/javase/6/docs/api/javax/swing/tree/DefaultTreeModel.html

after you add a node, then you need to tell the model that the node was added; which will tell the ui to refresh properly.

for example:

nodesWereInserted(TreeNode node, int[] childIndices)
          Invoke this method after you've inserted some TreeNodes into node.p
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号