DefaultMutableTreeNode

DefaultMutableTreeNode

我正在尝试在 java swing 中创建一个 JTree 现在我想在运行时更改节点文本

try
 {

int a=1,b=2,c=3;
 DefaultMutableTreeNode root =
new DefaultMutableTreeNode("A"+a);
DefaultMutableTreeNode child[]=new DefaultMutableTreeNode[1];
DefaultMutableTreeNode grandChild[]= new DefaultMutableTreeNode[1];

child[0] = new DefaultMutableTreeNode("Central Excise"+b);
  grandChild[0]=new DefaultMutableTreeNode("CE Acts: "+c);
child[0].add(grandChild[0]);
 root.add(child[0]);
tree = new JTree(root);
 }
 catch(Exception ex)

 {
  ex.printStackTrace()
 }

现在我想稍后我如何在子节点和大子节点中动态地和类似地将 A 1 更改为 2

最佳答案

您正在寻找 javax.swing.tree.DefaultMutableTreeNode.setUserObject(Object)

DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();
root.setUserObject("My label");
model.nodeChanged(root);

这假设您正在使用 DefautltTreeModel

10-08 19:43