[英]Can't set JTree nodes
Working on a IntelliJ Platform plugin and just trying to set the nodes in a JTree, but I can't get it to work.使用 IntelliJ 平台插件并尝试在 JTree 中设置节点,但我无法让它工作。
This is the JTree in the form, and here is the code:这是表单中的 JTree,下面是代码:
public class MyToolWindow {
private JTree tree1;
private JPanel myToolWindowContent;
public MyToolWindow(ToolWindow toolWindow) {
DefaultMutableTreeNode top = new DefaultMutableTreeNode("The Java Series");
//createNodes(top);
tree1 = new JTree(top);
}
public JPanel getContent() {
return myToolWindowContent;
}
When I run it, all I get is the boilerplate Tree:当我运行它时,我得到的只是样板树:
What am I doing wrong here??我在这里做错了什么?
Found a solution that works for me:找到了一个适合我的解决方案:
I checked the "Custom Create" box, and then implemented the createUIComponents
method in the code:我勾选了“自定义创建”框,然后在代码中实现了createUIComponents
方法:
private void createUIComponents() {
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
DefaultTreeModel treeModel = new DefaultTreeModel(root);
treeModel.setAsksAllowsChildren(true);
tree1 = new JTree(treeModel);
}
That gets me a base JTree configured however I need it.这让我配置了一个基本的 JTree,但是我需要它。 Then when my ToolWindow loads I can access the tree like this:然后,当我的 ToolWindow 加载时,我可以像这样访问树:
DefaultTreeModel model = (DefaultTreeModel)tree1.getModel();
DefaultMutableTreeNode root = (DefaultMutableTreeNode)model.getRoot();
root.add(new DefaultMutableTreeNode("another_child"));
model.reload(root);
Not 100% sure that is the best practice way, but its working for me.不是 100% 确定这是最佳实践方式,但它对我有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.