JTree

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;

public class JTreeDemo2 implements ActionListener,TreeModelListener{
	JLabel label = null;
	JTree tree = null;
	DefaultTreeModel treeModel = null;
	String nodeName = null;//原節點名稱
	JButton button1,button2,button3;
	
	public JTreeDemo2(){
		JFrame jf = new JFrame("JTreeDemo2");
		Container contentPane = jf.getContentPane();
		
		DefaultMutableTreeNode root = new DefaultMutableTreeNode("資源管理器");
		
		tree = new JTree(root);
		tree.setEditable(true);
		tree.addMouseListener(new MouseHandle2());
		treeModel = (DefaultTreeModel)tree.getModel();
		treeModel.addTreeModelListener(this);
		
		tree.addMouseListener(new MouseAdapter(){
			@Override
			public void mousePressed(MouseEvent e){
				TreePath treePath = tree.getPathForLocation(e.getX(), e.getY());
				if(treePath!=null){
					button1.setEnabled(true);
					button2.setEnabled(true);
				}else{
					button1.setEnabled(false);
					button2.setEnabled(false);
				}
			}
		});
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setViewportView(tree);
		
		JPanel panel = new JPanel();
		button1 = new JButton("新增節點");
		button1.addActionListener(this);
		button1.setEnabled(false);
		panel.add(button1);
		
		button2 = new JButton("刪除節點");
		button2.addActionListener(this);
		button2.setEnabled(false);
		panel.add(button2);
		
		button3 = new JButton("清除所有節點");
		button3.addActionListener(this);
		panel.add(button3);
		
		label = new JLabel("Action");
		contentPane.add(panel,BorderLayout.NORTH);
		contentPane.add(scrollPane,BorderLayout.CENTER);
		contentPane.add(label,BorderLayout.SOUTH);
		
		jf.pack();
		jf.setVisible(true);
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	public static void main(String...args){
		new JTreeDemo2();
	}
	
	
	@Override
	public void actionPerformed(ActionEvent e){
		if(e.getSource()==button1){
			DefaultMutableTreeNode parentNode = null;
			DefaultMutableTreeNode newNode = new DefaultMutableTreeNode("新增節點");
			newNode.setAllowsChildren(true);
			TreePath treePath = tree.getSelectionPath();
			if(treePath!=null){
				parentNode = (DefaultMutableTreeNode) treePath.getLastPathComponent();
				System.out.println("parentNode:"+parentNode.getUserObject());
				treeModel.insertNodeInto(newNode, parentNode, parentNode.getChildCount());
				tree.scrollPathToVisible(new TreePath(newNode.getPath()));
				label.setText("新增節點成功");
			}
		}
		if(e.getSource()==button2){
			TreePath treePath = tree.getSelectionPath();
			System.out.println(treePath);
			if(treePath!=null){
				DefaultMutableTreeNode selectionNode = (DefaultMutableTreeNode)treePath.getLastPathComponent();
				TreeNode parentNode = selectionNode.getParent();
				if(parentNode!=null){
					treeModel.removeNodeFromParent(selectionNode);
					label.setText("刪除節點成功");
				}
			}
		}
		if(e.getSource()==button3){
//			button1.setEnabled(false);
//			button2.setEnabled(false);
			DefaultMutableTreeNode root = (DefaultMutableTreeNode) treeModel.getRoot();
			root.removeAllChildren();
			treeModel.reload();
			tree.repaint();
			
			label.setText("清除所有節點成功");
		}
	}
	
	@Override
	public void treeNodesChanged(TreeModelEvent e){
		TreePath treePath = e.getTreePath();
		DefaultMutableTreeNode node = (DefaultMutableTreeNode)treePath.getLastPathComponent();
		try{
			int[] index = e.getChildIndices();
			node = (DefaultMutableTreeNode) node.getChildAt(index[0]);
		}catch(NullPointerException ne){
			System.out.println("你選擇的是根節點,它沒有子節點");
		}
		label.setText(nodeName+"更改數據為:"+(String)node.getUserObject());
	}
	
	@Override
	public void treeNodesInserted(TreeModelEvent e){
		System.out.println("New Node Inserted");
	}
	@Override
	public void treeNodesRemoved(TreeModelEvent e){
		System.out.println("Node Deleted");
	}
	@Override
	public void treeStructureChanged(TreeModelEvent e){
		System.out.println("structure changed");
	}
	
	class MouseHandle2 extends MouseAdapter{
		@Override
		public void mousePressed(MouseEvent e){
			try{
				TreePath treePath = tree.getSelectionPath();
				
				/*JTree tree = (JTree)e.getSource();
				int rowLocation = tree.getRowLocation(e.getX(),e.getY());
				TreePath treePath = tree.getPathForRow(rowLocation);
				*/
				DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode)treePath.getLastPathComponent();
				
//				TreeNode treeNode2 = (DefaultMutableTreeNode)treePath.getLastPathComponent();
				nodeName = (String) treeNode.getUserObject();
			}catch(NullPointerException ne){
				System.out.println("nullpointer");
			}
		}
	}
}


參考:深入淺出Java Swing程序設計

你可能感兴趣的:(swing)