简体   繁体   English

在 Java 中手动重命名 JTree 节点

[英]Renaming the JTree Node Manually in Java

I have created a jtree with the root node "RootNode" and some other nodes like "Node1","Node2", Node3".我创建了一个带有根节点“RootNode”和其他一些节点(如“Node1”、“Node2”、“Node3”)的 jtree。

After creation of tree I want to rename the rootnode or any other node by manually.创建树后,我想手动重命名根节点或任何其他节点。 We can rename the node dynamically by using the method我们可以使用方法动态重命名节点

 jtee.setEditable(true);

But I want to change the name in manualy by the code level但我想通过代码级别手动更改名称

like喜欢

 someMethod(Arg1 oldNodeName,Arg2 newNodeName)

Is there any way to do this?有没有办法做到这一点?

Assuming you are using a DefaultMutableTreeNode you could just change the UserObject (whose toString() method is what is used to display the node name) by calling: setUserObject() on the node you want to change.假设您使用的是 DefaultMutableTreeNode,您可以通过在要更改的节点上调用setUserObject()来更改 UserObject(其 toString() 方法用于显示节点名称)。

I'll assume you're using a tree with a DefaultTreeModel , using instances of DefaultMutableTreeNode .我假设您正在使用具有DefaultTreeModel的树,并使用DefaultMutableTreeNode的实例。

You'll have to iterate through the tree nodes and find the one which has the oldNodeName as user object, then change its user object to newNodeName , and call the method nodeChanged of the tree model.您必须遍历树节点并找到具有oldNodeName为用户 object 的节点,然后将其用户 object 更改为newNodeName ,并调用树 Z20F35E630DAF449DFA4C3F688F5C3 的方法nodeChanged

To properly rename a DefaultMutableTreeNode , you must set it's new user object as well as notify the JTree s table model the node changed so it can resize it for the shorter/longer text.要正确重命名DefaultMutableTreeNode ,您必须将其设置为新用户 object 并通知JTree的表 model 节点已更改,以便它可以针对更短/更长的文本调整其大小。

Assuming your tree is using a DefaultTreeModel , use this:假设您的树使用DefaultTreeModel ,请使用:

public void renameNode(JTree tree, DefaultMutableTreeNode node, Object new_user_object) {
     node.setUserObject(new_user_object);
     ((DefaultTreeModel) tree.getModel()).nodeChanged(node);
}

It does change the UI, if:如果出现以下情况,它确实会更改 UI:

  • you are using a DefaultTreeModel model您正在使用 DefaultTreeModel model
  • you actually change the UserObject你实际上改变了 UserObject

This is to say something like this:这就是说这样的话:

// TheNode is a CustomMutableTreeNode (extending DefaultMutableTreeNode)
// and points to the selected node to alter
String NewNodeName = Dlg.NewNodeName.getText();
if(!NewNodeName.isEmpty()) {
    ON.setName(NewNodeName); // ON is the real source data
    TheNode.setUserObject(NewNodeName);
    ((DefaultTreeModel)JSONTree.getModel()).nodeChanged(TheNode);
}

It took me quite a while to figure out that changing the source data (in ON) did nothing on the UI, even with a repaint();.我花了很长时间才弄清楚更改源数据(在 ON 中)对 UI 没有任何作用,即使使用 repaint(); 也是如此。 You really have to update theUserObject (a String from the DefaultMutableTreeNode)真的必须更新用户对象(来自 DefaultMutableTreeNode 的字符串)

Once this done, it's the easiest and most elegant solution IMO.一旦完成,它就是 IMO 最简单、最优雅的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM