JTree 使用 arraylist 更改动态更新

JTree dynamically updating with arraylist changes

我在寻找答案,但找不到任何解决方法。我有两个数组列表,其中包含用户单击后动态更改的字符串,现在创建 JTree 将显示一个列表中的元素取决于另一个列表中的某种类型。我遇到了一个问题,因为我总是在我的列表中添加和删除某些内容,并且需要 JTree 来显示这些更改。它会正确地添加 List 中的所有元素,直到我展开,之后就不可能更新 JTree 的内容,至少对我来说是这样。如果我清空我的列表,它会显示为折叠状态,但所有内容仍显示为展开状态,即使它在控制台中显示 childcount 为 0,如果我向列表添加内容,它也会添加但不会显示。 所以基本上问题是直观地显示 JTree 的变化。

public class JSelectedTree extends JPanel{
          private JTree selectionTree;
          private DefaultTreeModel treeModel;
          public DefaultMutableTreeNode point;
          public DefaultMutableTreeNode polygon;
          public DefaultMutableTreeNode line;
          public JSelectedTree() {
            initGUI();
          }

          public void initGUI() {
                DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
                point = new DefaultMutableTreeNode("Point");
                polygon = new DefaultMutableTreeNode("Polygon");
                line = new DefaultMutableTreeNode("Line");
                root.add(point);
                root.add(polygon);
                root.add(line);
                treeModel = new DefaultTreeModel(root);
                selectionTree = new JTree(root);
                selectionTree.setRootVisible(false);
                selectionTree.setShowsRootHandles(false);
                selectionTree.getSelectionModel().setSelectionMode(
                    TreeSelectionModel.SINGLE_TREE_SELECTION);
                JScrollPane scroll = new JScrollPane(selectionTree);
                scroll.setBorder(BorderFactory.createTitledBorder("Selected"));
                setLayout(new BorderLayout());
                add(scroll, BorderLayout.CENTER);
              }
          public void resetTree(){
              if(!MultiSelectTool.selectedObject.isEmpty()){
                  polygon.removeAllChildren();
                  point.removeAllChildren();
                  line.removeAllChildren();
                  treeModel.reload();
                  int n = MultiSelectTool.selectedObject.size();
                  for(int i = 0; i < n; i++){
                      if(MultiSelectTool.selectedCoords.get(i).contains("(((")){
                          DefaultMutableTreeNode child = new DefaultMutableTreeNode(MultiSelectTool.selectedObject.get(i));
                          treeModel.insertNodeInto(child, polygon, polygon.getChildCount());
                      }else if(MultiSelectTool.selectedCoords.get(i).contains("((")){
                          DefaultMutableTreeNode child = new DefaultMutableTreeNode(MultiSelectTool.selectedObject.get(i));
                          treeModel.insertNodeInto(child, line, line.getChildCount());
                      }else{
                          DefaultMutableTreeNode child = new DefaultMutableTreeNode(MultiSelectTool.selectedObject.get(i));
                          treeModel.insertNodeInto(child, point, point.getChildCount());
                      }
                  }
              }else{
                  polygon.removeAllChildren();
                  point.removeAllChildren();
                  line.removeAllChildren();
                  treeModel.reload();
              }
          }
}

尝试使用 MVC 模式以便更新视图:)

通过从 DynamicTreeDemo 中提取一些部分来解决,如果有人需要这样的东西,这里是代码

public class JSelectedTree extends JPanel{
          static JButton addButton; 
          public static DynamicTree treePanel;
          public DefaultMutableTreeNode point;
          public DefaultMutableTreeNode polygon;
          public DefaultMutableTreeNode line;
            DefaultMutableTreeNode p1, p2, p3;
          public JSelectedTree() {

                treePanel = new DynamicTree();
                populateTree(treePanel);
                addButton = new JButton("Add");
                addButton.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                          if(!MultiSelectTool.selectedObject.isEmpty()){
                              treePanel.clear();
                              populateTree(treePanel);
                              int n = MultiSelectTool.selectedObject.size();
                              for(int i = 0; i < n; i++){
                                  if(MultiSelectTool.selectedCoords.get(i).contains("(((")){

                                      String child = new String(MultiSelectTool.selectedObject.get(i));
                                      treePanel.addObject(p1,child, true);
                                  }else if(MultiSelectTool.selectedCoords.get(i).contains("((")){
                                      String child = new String(MultiSelectTool.selectedObject.get(i));
                                      treePanel.addObject(p2,child, true);
                                  }else{
                                      String child = new String(MultiSelectTool.selectedObject.get(i));
                                      treePanel.addObject(p3,child, true);
                                  }
                              }
                          }else{
                                treePanel.clear();
                                  populateTree(treePanel);
                            }
                    }
                });
                treePanel.setPreferredSize(new Dimension(200, 200));
                add(treePanel, BorderLayout.CENTER);
                treePanel.setBorder(BorderFactory.createTitledBorder("Selected"));
          }

          public void populateTree(DynamicTree treePanel) {
                String p1Name = new String("Polygon");
                String p2Name = new String("Point");
                String p3Name = new String("Line");



                p1 = treePanel.addObject(null, p1Name, true);
                p2 = treePanel.addObject(null, p2Name, true);
                p3 = treePanel.addObject(null, p3Name, true);

              }
}
          class DynamicTree extends JPanel {
              protected DefaultMutableTreeNode rootNode;
              protected DefaultTreeModel treeModel;
              protected JTree tree;
              private Toolkit toolkit = Toolkit.getDefaultToolkit();

              public DynamicTree() {
                super(new GridLayout(1, 0));

                rootNode = new DefaultMutableTreeNode("Root Node");
                treeModel = new DefaultTreeModel(rootNode);

                tree = new JTree(treeModel);
                tree.setEditable(true);
                tree.getSelectionModel().setSelectionMode(
                    TreeSelectionModel.SINGLE_TREE_SELECTION);
                tree.setShowsRootHandles(true);

                JScrollPane scrollPane = new JScrollPane(tree);
                add(scrollPane);
              }

              /** Remove all nodes except the root node. */
              public void clear() {
                rootNode.removeAllChildren();
                treeModel.reload();
              }

              /** Remove the currently selected node. */
              public void removeCurrentNode() {
                TreePath currentSelection = tree.getSelectionPath();
                if (currentSelection != null) {
                  DefaultMutableTreeNode currentNode = (DefaultMutableTreeNode) (currentSelection
                      .getLastPathComponent());
                  MutableTreeNode parent = (MutableTreeNode) (currentNode.getParent());
                  if (parent != null) {
                    treeModel.removeNodeFromParent(currentNode);
                    return;
                  }
                }

                // Either there was no selection, or the root was selected.
                toolkit.beep();
              }

              public DefaultMutableTreeNode addObject(DefaultMutableTreeNode parent,
                  Object child) {
                return addObject(parent, child, false);
              }

              public DefaultMutableTreeNode addObject(DefaultMutableTreeNode parent,
                  Object child, boolean shouldBeVisible) {
                DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(child);

                if (parent == null) {
                  parent = rootNode;
                }

                // It is key to invoke this on the TreeModel, and NOT DefaultMutableTreeNode
                treeModel.insertNodeInto(childNode, parent, parent.getChildCount());

                // Make sure the user can see the lovely new node.
                if (shouldBeVisible) {
                  tree.scrollPathToVisible(new TreePath(childNode.getPath()));
                }
                return childNode;
              }

              class MyTreeModelListener implements TreeModelListener {
                public void treeNodesChanged(TreeModelEvent e) {
                  DefaultMutableTreeNode node;
                  node = (DefaultMutableTreeNode) (e.getTreePath().getLastPathComponent());

                  /*
                   * If the event lists children, then the changed node is the child of the
                   * node we've already gotten. Otherwise, the changed node and the
                   * specified node are the same.
                   */

                  int index = e.getChildIndices()[0];
                  node = (DefaultMutableTreeNode) (node.getChildAt(index));

                  System.out.println("The user has finished editing the node.");
                  System.out.println("New value: " + node.getUserObject());
                }

                public void treeNodesInserted(TreeModelEvent e) {
                }

                public void treeNodesRemoved(TreeModelEvent e) {
                }

                public void treeStructureChanged(TreeModelEvent e) {
                }
              }
            }