无法设置 JTree 节点

问题描述

正在开发 IntelliJ 平台插件并尝试在 JTree 中设置节点,但我无法让它工作。

enter image description here

这是表单中的JTree,代码如下:

data = data.frame(gm1 = c("Lin","Bill","Cass","Dan"),gm2 = c("Bill","Lin","Lin"),gm3 = c("Cass","Dan","Cass"),gm4 = c("Dan","Bill"),crit1_1 = c(5,2,4,4),crit1_2 = c(5,3,3),crit1_3 = c(4,crit1_4 = c(5,5,4))



cols_gm <- sapply(data,is.character)
cols_crit <- sapply(data,is.numeric)


crit <- data[cols_crit]
gm <- data[cols_gm]
nm <- unique(unlist(gm))


sapply(nm,function(x) {
  coord <- matrix(c(seq(nrow(data)),max.col(gm == x)),nrow = nrow(data))
  coord <- coord[!(coord[,2] == 1),]
  mean(crit[coord])
})
#>      Lin     Bill     Cass      Dan 
#> 3.333333 4.000000 4.333333 3.666667

当我运行它时,我得到的只是样板树:

enter image description here

在这里做错了什么??

解决方法

找到适合我的解决方案:

enter image description here

我选中了“自定义创建”框,然后在代码中实现了 createUIComponents 方法:

private void createUIComponents() {
    DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
    DefaultTreeModel treeModel = new DefaultTreeModel(root);
    treeModel.setAsksAllowsChildren(true);
    tree1 = new JTree(treeModel);
  }

这让我得到了一个基本的 JTree 配置,但我需要它。然后当我的 ToolWindow 加载时,我可以像这样访问树:

DefaultTreeModel model = (DefaultTreeModel)tree1.getModel();
DefaultMutableTreeNode root = (DefaultMutableTreeNode)model.getRoot();
root.add(new DefaultMutableTreeNode("another_child"));
model.reload(root);

不能 100% 确定这是最佳实践方式,但它对我有用。