向非叶节点动态添加节点不起作用 | J树

Posted

技术标签:

【中文标题】向非叶节点动态添加节点不起作用 | J树【英文标题】:Dynamic addition of node to non-leaf node not working | JTree 【发布时间】:2014-05-06 22:04:56 【问题描述】:

我正在尝试将节点动态添加到弹出组件和 JMenuItem 组件的单击操作事件中的 JTree。我右键单击节点组件并添加另一个节点。添加节点适用于叶节点,但不适用于非叶节点。我不知道代码有什么问题。好像添加了节点,但 Gui 没有更新。

平台: 视窗 7

Java java版本“1.7.0_17” Java(TM) SE 运行时环境 (build 1.7.0_17-b02) Java HotSpot(TM) 64 位服务器 VM(内部版本 23.7-b01,混合模式)

package test;

/**
 *
 * @author Rajan
 */

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.MutableTreeNode;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;

/**
 *
 * @author Rajan Prasad Upadhyay
 */
public class TreeTest 

    String name = "name";

    public static void main(String[] args) 
        javax.swing.JFrame topFrame = new JFrame();
        javax.swing.JPanel topPanel = new JPanel();
        javax.swing.JScrollPane leftScrollPane = new JScrollPane();
        javax.swing.JTree leftTree = new JTree();

        topFrame.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        //layouts
        topFrame.setLayout(new BorderLayout());
        topPanel.setLayout(new BorderLayout());

        // addition of components
//        topPanel.setMaximumSize(new Dimension(Integer.MAX_VALUE,Integer.MAX_VALUE));
        topPanel.add(leftScrollPane);

        leftScrollPane.getViewport().add(leftTree);

        addExperimentEvents(leftTree);

        topFrame.setSize(600, 500);
        topFrame.setContentPane(topPanel);
        topFrame.setVisible(true);
    

    public static void addExperimentEvents(JTree tree) 
        MouseAdapter ma = new MouseAdapter() 
            private void myPopupEvent(MouseEvent e) 
                int x = e.getX();
                int y = e.getY();
                final JTree tree = (JTree) e.getSource();
                TreePath path = tree.getPathForLocation(x, y);
                if (path == null) 
                    return;
                

                tree.setSelectionPath(path);

                Object obj = (Object) path.getLastPathComponent();

                String label = "Add:";// + obj.toString();
                JPopupMenu popup = new JPopupMenu("Menu");

                JMenu sectionsMenu = new JMenu("Sections");
                JMenuItem menuItem1 = new JMenuItem("Item1");
                sectionsMenu.add(menuItem1);
                JMenuItem menuItem2 = new JMenuItem("Item2");
                sectionsMenu.add(menuItem2);

                JMenuItem item = new JMenuItem(label);
                item.addActionListener(new ActionListener() 

                    @Override
                    public void actionPerformed(ActionEvent ae) 
                        System.out.println("Editing the tree");
                        DefaultMutableTreeNode selNode = (DefaultMutableTreeNode) tree
                                .getLastSelectedPathComponent();
                        TreeNode root = (TreeNode) tree.getModel().getRoot();
                        DefaultTreeModel model = new DefaultTreeModel(root);

                        if (selNode != null) 
                            DefaultMutableTreeNode newNode = new DefaultMutableTreeNode("New Node" + (selNode.getChildCount() + 1));
                            model.insertNodeInto(newNode, selNode, selNode.getChildCount());//
                            //model.insertNodeInto(newNode, (MutableTreeNode) root, root.getChildCount());
                            tree.expandPath(new TreePath(model.getPathToRoot(newNode.getParent())));

                            TreeNode[] nodes = model.getPathToRoot(newNode);
                            TreePath path = new TreePath(nodes);

                            System.out.println(selNode.getChildCount() + ", " + path);
                            tree.scrollPathToVisible(path);
                            tree.setSelectionPath(path);
                            tree.startEditingAtPath(path);
                            model.reload((TreeNode) model.getRoot());
                        else
                            System.out.println("selNode is null");
                        
                    
                );
                //addHoverAction(item);//
                //item.add(new JMenuItem("page"));

                popup.add(item);
                popup.add(new JMenuItem("Move: "));
                popup.add(sectionsMenu);
                popup.show(tree, x, y);
            

            public void mousePressed(MouseEvent e) 
                if (e.isPopupTrigger()) 
                    myPopupEvent(e);
                
            

            public void mouseReleased(MouseEvent e) 
                if (e.isPopupTrigger()) 
                    myPopupEvent(e);
                
            
        ;
        tree.addMouseListener(ma);
    

    public static void addHoverAction(final JComponent com) 

        MouseAdapter adapter = new MouseAdapter() 
            public void mouseEntered(java.awt.event.MouseEvent evt) 
                System.out.println("mouse Entered: ");
                if (true) 
                    JPopupMenu pop = new JPopupMenu();
                    pop.add(new JMenuItem("Node"));

                    pop.show(com.getParent(), com.getWidth(), com.getHeight());
                
            
        ;
        com.addMouseListener(adapter);
    

非常感谢您提供解决方案和问题描述。

【问题讨论】:

【参考方案1】:

使用下一行

DefaultTreeModel model = (DefaultTreeModel) tree.getModel();

而不是

DefaultTreeModel model = new DefaultTreeModel(root);

最后展开想要的节点。

这里是关于How to expand or collapse all JTree nodes?的示例代码

【讨论】:

【参考方案2】:

有一个Swing tutorial that does exactly that。您可以查看他们用于动态添加、删除和编辑树的完整代码。他们的代码很短。

【讨论】:

以上是关于向非叶节点动态添加节点不起作用 | J树的主要内容,如果未能解决你的问题,请参考以下文章

将单个令牌节点添加到现有的 datastax cassandra 集群和数据传输不起作用

如何在maven中找到依赖项,依赖项:树不起作用

覆盖节点 eventEmitter.on 并关闭日志记录不起作用

节点 package.json “bin” 值,命令不起作用

@babel/plugin-syntax-dynamic-import 在导入的节点模块中不起作用

节点链接不起作用