Java JTree_5

Java JTree_5

package com.han;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Enumeration;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;

public class JTree_5 extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7187852901157241733L;

	public JTree_5() {
		// TODO Auto-generated constructor stub
		DefaultMutableTreeNode root = new DefaultMutableTreeNode("倾城爱家");
		DefaultMutableTreeNode nodeA = new DefaultMutableTreeNode("经理办公室");
		DefaultMutableTreeNode nodeB = new DefaultMutableTreeNode("人事部");
		DefaultMutableTreeNode nodeC = new DefaultMutableTreeNode("技术部");
		DefaultMutableTreeNode nodeD = new DefaultMutableTreeNode("服务部");
		DefaultMutableTreeNode nodeE = new DefaultMutableTreeNode("推广部");
		
		DefaultMutableTreeNode nodeCA = new DefaultMutableTreeNode("网络维护部");
		DefaultMutableTreeNode nodeCB = new DefaultMutableTreeNode("应用开发部");
		
		DefaultMutableTreeNode nodeDA = new DefaultMutableTreeNode("网络服务部");
		DefaultMutableTreeNode nodeDB = new DefaultMutableTreeNode("内勤开发部");
		
		root.add(nodeA);
		root.add(nodeB);
		root.add(nodeC);
		root.add(nodeD);
		root.add(nodeE);
		nodeC.add(nodeCA);
		nodeC.add(nodeCB);
		nodeD.add(nodeDA);
		nodeD.add(nodeDB);
		
		final DefaultTreeModel treeModel = new DefaultTreeModel(root, true);
		final JTree tree =new JTree(treeModel);
		
		Container container = getContentPane();
		container.add(tree, BorderLayout.CENTER);
		
		@SuppressWarnings("rawtypes")
		Enumeration enumeration = root.preorderEnumeration();
		while (enumeration.hasMoreElements()) {
			DefaultMutableTreeNode node = (DefaultMutableTreeNode) enumeration.nextElement();
			tree.expandPath(new TreePath(node.getPath()));
		}
		
		JPanel panel = new JPanel(new GridLayout(1, 5, 10, 10));
		container.add(panel, BorderLayout.SOUTH);
		JLabel label = new JLabel("名称:");
		final JTextField textField = new JTextField("测试");
		JButton button1 = new JButton("添加");
		JButton button2 = new JButton("修改");
		JButton button3 = new JButton("删除");
		panel.add(label);
		panel.add(textField);
		panel.add(button1);
		panel.add(button2);
		panel.add(button3);
		
		// add
		button1.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				DefaultMutableTreeNode node = new DefaultMutableTreeNode(textField.getText());
				TreePath path = tree.getSelectionPath();
				DefaultMutableTreeNode parent = (DefaultMutableTreeNode) path.getLastPathComponent();
				treeModel.insertNodeInto(node, parent, parent.getChildCount());
				TreePath newNodePath = path.pathByAddingChild(node);
				if (!tree.isVisible(newNodePath)) {
					tree.makeVisible(newNodePath);
				}
				tree.setSelectionPath(newNodePath);
			}
			
		});
		
		// modify
		button2.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				TreePath path = tree.getSelectionPath();
				DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
				node.setUserObject(textField.getText());
				treeModel.nodeChanged(node);				
			}
			
		});
		
		// delete
		button3.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				TreePath path = tree.getSelectionPath();
				DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
				if (!node.isRoot()) {
					DefaultMutableTreeNode nextSelectedNode = node.getNextSibling();
					if (nextSelectedNode == null) {
						nextSelectedNode = (DefaultMutableTreeNode) node.getParent();
					}
					tree.setSelectionPath(new TreePath(nextSelectedNode.getPath()));
					treeModel.removeNodeFromParent(node);
				}
			}
			
		});
		
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		JTree_5 instance = new JTree_5();
		instance.setTitle("维护企业架构");
		instance.setVisible(true);
		instance.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		instance.pack();
	}

}


你可能感兴趣的:(JTree)