JTree 添加删除节点

来源:互联网 发布:单片机上拉电阻原理图 编辑:程序博客网 时间:2024/06/11 02:17


[java] view plaincopy
  1. import java.awt.BorderLayout;  
  2. import java.awt.event.ActionEvent;  
  3. import java.awt.event.ActionListener;  
  4. import java.util.Enumeration;  
  5.   
  6. import javax.swing.BorderFactory;  
  7. import javax.swing.JButton;  
  8. import javax.swing.JFrame;  
  9. import javax.swing.JPanel;  
  10. import javax.swing.JScrollPane;  
  11. import javax.swing.JTextField;  
  12. import javax.swing.JTree;  
  13. import javax.swing.tree.DefaultMutableTreeNode;  
  14. import javax.swing.tree.DefaultTreeModel;  
  15. import javax.swing.tree.MutableTreeNode;  
  16. import javax.swing.tree.TreeNode;  
  17. import javax.swing.tree.TreePath;  
  18.   
  19. public class Main extends JFrame  
  20. {  
  21.   
  22.     private DefaultMutableTreeNode m_rootNode = new DefaultMutableTreeNode("AA");  
  23.   
  24.     private DefaultTreeModel m_model = new DefaultTreeModel(m_rootNode);  
  25.   
  26.     private JTree m_tree = new JTree(m_model);  
  27.   
  28.     private JButton m_addButton = new JButton("Add Node");  
  29.   
  30.     private JButton m_delButton = new JButton("Delete Node");  
  31.   
  32.     private JButton m_searchButton = new JButton("Search Node");  
  33.   
  34.     private JButton m_searchAndDeleteButton = new JButton(  
  35.             "Search and Delete Node");  
  36.   
  37.     private JTextField m_searchText;  
  38.   
  39.     public Main()  
  40.     {  
  41.         DefaultMutableTreeNode forums = new DefaultMutableTreeNode("A");  
  42.         forums.add(new DefaultMutableTreeNode("B"));  
  43.         DefaultMutableTreeNode articles = new DefaultMutableTreeNode("E");  
  44.         articles.add(new DefaultMutableTreeNode("F"));  
  45.         DefaultMutableTreeNode examples = new DefaultMutableTreeNode("G");  
  46.         examples.add(new DefaultMutableTreeNode("H"));  
  47.   
  48.         m_rootNode.add(forums);  
  49.         m_rootNode.add(articles);  
  50.         m_rootNode.add(examples);  
  51.   
  52.         m_tree.setEditable(true);  
  53.         m_tree.setSelectionRow(0);  
  54.   
  55.         JScrollPane scrollPane = new JScrollPane(m_tree);  
  56.         getContentPane().add(scrollPane, BorderLayout.CENTER);  
  57.   
  58.         JPanel panel = new JPanel();  
  59.   
  60.         m_addButton.addActionListener(new ActionListener()  
  61.         {  
  62.             public void actionPerformed(ActionEvent e)  
  63.             {  
  64.                 DefaultMutableTreeNode selNode = (DefaultMutableTreeNode) m_tree  
  65.                         .getLastSelectedPathComponent();  
  66.                 if (selNode == null)  
  67.                 {  
  68.                     return;  
  69.                 }  
  70.                 DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(  
  71.                         "New Node");  
  72.                 m_model.insertNodeInto(newNode, selNode, selNode  
  73.                         .getChildCount());  
  74.   
  75.                 TreeNode[] nodes = m_model.getPathToRoot(newNode);  
  76.                 TreePath path = new TreePath(nodes);  
  77.                 m_tree.scrollPathToVisible(path);  
  78.                 m_tree.setSelectionPath(path);  
  79.                 m_tree.startEditingAtPath(path);  
  80.             }  
  81.         });  
  82.         panel.add(m_addButton);  
  83.   
  84.         m_delButton.addActionListener(new ActionListener()  
  85.         {  
  86.             public void actionPerformed(ActionEvent e)  
  87.             {  
  88.                 DefaultMutableTreeNode selNode = (DefaultMutableTreeNode) m_tree  
  89.                         .getLastSelectedPathComponent();  
  90.                 removeNode(selNode);  
  91.             }  
  92.         });  
  93.         panel.add(m_delButton);  
  94.   
  95.         JPanel searchPanel = new JPanel();  
  96.         searchPanel.setBorder(BorderFactory.createEtchedBorder());  
  97.   
  98.         m_searchText = new JTextField(10);  
  99.         searchPanel.add(m_searchText);  
  100.   
  101.         m_searchButton.addActionListener(new ActionListener()  
  102.         {  
  103.             public void actionPerformed(ActionEvent e)  
  104.             {  
  105.                 DefaultMutableTreeNode node = searchNode(m_searchText.getText());  
  106.                 if (node != null)  
  107.                 {  
  108.                     TreeNode[] nodes = m_model.getPathToRoot(node);  
  109.                     TreePath path = new TreePath(nodes);  
  110.                     m_tree.scrollPathToVisible(path);  
  111.                     m_tree.setSelectionPath(path);  
  112.                 }  
  113.                 else  
  114.                 {  
  115.                     System.out.println("Node with string "  
  116.                             + m_searchText.getText() + " not found");  
  117.                 }  
  118.             }  
  119.         });  
  120.         searchPanel.add(m_searchButton);  
  121.   
  122.         m_searchAndDeleteButton.addActionListener(new ActionListener()  
  123.         {  
  124.             public void actionPerformed(ActionEvent e)  
  125.             {  
  126.                 DefaultMutableTreeNode node = searchNode(m_searchText.getText());  
  127.                 if (node != null)  
  128.                 {  
  129.                     removeNode(node);  
  130.                 }  
  131.                 else  
  132.                 {  
  133.                     System.out.println("Node with string "  
  134.                             + m_searchText.getText() + " not found");  
  135.                 }  
  136.             }  
  137.         });  
  138.         searchPanel.add(m_searchAndDeleteButton);  
  139.         panel.add(searchPanel);  
  140.         getContentPane().add(panel, BorderLayout.SOUTH);  
  141.         setSize(700400);  
  142.         setVisible(true);  
  143.     }  
  144.   
  145.     public DefaultMutableTreeNode searchNode(String nodeStr)  
  146.     {  
  147.         DefaultMutableTreeNode node = null;  
  148.         Enumeration e = m_rootNode.breadthFirstEnumeration();  
  149.         while (e.hasMoreElements())  
  150.         {  
  151.             node = (DefaultMutableTreeNode) e.nextElement();  
  152.             if (nodeStr.equals(node.getUserObject().toString()))  
  153.             {  
  154.                 return node;  
  155.             }  
  156.         }  
  157.         return null;  
  158.     }  
  159.   
  160.     public void removeNode(DefaultMutableTreeNode selNode)  
  161.     {  
  162.         if (selNode == null)  
  163.         {  
  164.             return;  
  165.         }  
  166.         MutableTreeNode parent = (MutableTreeNode) (selNode.getParent());  
  167.         if (parent == null)  
  168.         {  
  169.             return;  
  170.         }  
  171.         MutableTreeNode toBeSelNode = getSibling(selNode);  
  172.         if (toBeSelNode == null)  
  173.         {  
  174.             toBeSelNode = parent;  
  175.         }  
  176.         TreeNode[] nodes = m_model.getPathToRoot(toBeSelNode);  
  177.         TreePath path = new TreePath(nodes);  
  178.         m_tree.scrollPathToVisible(path);  
  179.         m_tree.setSelectionPath(path);  
  180.         m_model.removeNodeFromParent(selNode);  
  181.     }  
  182.   
  183.     private MutableTreeNode getSibling(DefaultMutableTreeNode selNode)  
  184.     {  
  185.         MutableTreeNode sibling = (MutableTreeNode) selNode  
  186.                 .getPreviousSibling();  
  187.         if (sibling == null)  
  188.         {  
  189.             sibling = (MutableTreeNode) selNode.getNextSibling();  
  190.         }  
  191.         return sibling;  
  192.     }  
  193.   
  194.     public static void main(String[] arg)  
  195.     {  
  196.         Main editableTree = new Main();  
  197.     }  
  198. }  
0 0
原创粉丝点击