import java.awt.BorderLayout; import java.awt.Component; import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTree; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.DefaultTreeModel; import javax.swing.tree.TreeNode; import javax.swing.tree.TreePath; /** */ /** * */ /** */ /** * @author tao.ni * */ public class SimpleTreeFrame extends JFrame ... { private static final int WIDTH = 300; private static final int HEIGHT = 200; private DefaultTreeModel treeModel; private JTree tree; public SimpleTreeFrame()...{ setTitle("SimpleTree"); setSize(WIDTH,HEIGHT); treeModel = new DefaultTreeModel(initTree()); tree = new JTree(treeModel); Container contentPans = getContentPane(); contentPans.add(new JScrollPane(tree)); makeButtons(); } public DefaultMutableTreeNode initTree()...{ DefaultMutableTreeNode root = new DefaultMutableTreeNode("World"); String countrys[] = ...{"USA"}; String states[] = ...{"California"}; String citys[] = ...{"San Jose","Cupertino"}; for(int i =0;i<countrys.length ;i++)...{ DefaultMutableTreeNode country = new DefaultMutableTreeNode(countrys[i]); for(int j =0;j<states.length ;j++)...{ DefaultMutableTreeNode state = new DefaultMutableTreeNode(states[j]); for(int k =0;k<citys.length ;k++)...{ DefaultMutableTreeNode city = new DefaultMutableTreeNode(citys[k]); state.add(city); } country.add(state); } root.add(country); } return root; } public void makeButtons()...{ JPanel panel = new JPanel(); JButton addSiblingButton = new JButton("Add Sibling"); addSiblingButton.addActionListener(new ActionListener()...{ public void actionPerformed(ActionEvent event) ...{ DefaultMutableTreeNode selectNode = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent(); if(selectNode == null)...{ return; } DefaultMutableTreeNode parent = (DefaultMutableTreeNode)selectNode.getParent(); if(parent == null)...{ return; } DefaultMutableTreeNode newNode = new DefaultMutableTreeNode("new"); int selectNodeIndex = parent.getIndex(selectNode); treeModel.insertNodeInto(newNode,parent,selectNodeIndex +1); TreeNode nodes[] = treeModel.getPathToRoot(newNode); TreePath path = new TreePath(nodes); tree.scrollPathToVisible(path); } }); panel.add(addSiblingButton); JButton addChildButton = new JButton("Add Child"); addChildButton.addActionListener(new ActionListener()...{ public void actionPerformed(ActionEvent event) ...{ DefaultMutableTreeNode selectNode = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent(); if(selectNode == null)...{ return; } DefaultMutableTreeNode newNode = new DefaultMutableTreeNode("new"); int selectNodeIndex = selectNode.getChildCount();//parent.getIndex(selectNode); treeModel.insertNodeInto(newNode,selectNode,selectNodeIndex); TreeNode nodes[] = treeModel.getPathToRoot(newNode); TreePath path = new TreePath(nodes); tree.scrollPathToVisible(path); } }); panel.add(addChildButton); JButton deleteButton = new JButton("Delete"); deleteButton.addActionListener(new ActionListener()...{ public void actionPerformed(ActionEvent e) ...{ DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent(); if(selectedNode == null)...{ return; } treeModel.removeNodeFromParent(selectedNode); } }); panel.add(deleteButton); getContentPane().add(panel,BorderLayout.SOUTH); }}