JTree

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

public class JTreeDemo1 implements TreeModelListener{

	JLabel label = null;
	String nodeName = null;//原有節點名稱
	public JTreeDemo1(){
		JFrame jf = new JFrame("JTreeDemo1");
		Container contentPane = jf.getContentPane();
		
		DefaultMutableTreeNode root = new DefaultMutableTreeNode("資源管理器");
		DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("我的公文包");
		DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("我的電腦");
		DefaultMutableTreeNode node3 = new DefaultMutableTreeNode("收藏夾");
		DefaultMutableTreeNode node4 = new DefaultMutableTreeNode("Readme");
		root.add(node1);
		root.add(node2);
		root.add(node3);
		root.add(node4);
		
		DefaultMutableTreeNode leafnode = new DefaultMutableTreeNode("公司文件");
		node1.add(leafnode);
		leafnode = new DefaultMutableTreeNode("私人文件");
		node1.add(leafnode);
		
		leafnode = new DefaultMutableTreeNode("本機磁盤C");
		node2.add(leafnode);
		leafnode = new DefaultMutableTreeNode("本機磁盤D");
		node2.add(leafnode);
		leafnode = new DefaultMutableTreeNode("本機磁盤E");
		node2.add(leafnode);

		leafnode = new DefaultMutableTreeNode("網站列表");
		node3.add(leafnode);
		leafnode = new DefaultMutableTreeNode("奇摩站");
		node3.add(leafnode);
		leafnode = new DefaultMutableTreeNode("網絡書店");
		node3.add(leafnode);
		
		JTree tree = new JTree(root);
		tree.setEditable(true);//設置樹可編輯
		tree.addMouseListener(new MouseHandle());
		DefaultTreeModel treeModel = (DefaultTreeModel)tree.getModel();
		treeModel.addTreeModelListener(this);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.getViewport().add(tree);
		//scrollPane.setViewportview(tree);
		
		label = new JLabel("更改數據為:");
		contentPane.add(scrollPane,BorderLayout.CENTER);
		contentPane.add(label,BorderLayout.SOUTH);
		jf.pack();
		jf.setVisible(true);
		jf.addWindowListener(new WindowAdapter(){
			@Override
			public void windowClosing(WindowEvent e){
				System.exit(0);
			}
		});
	}
	@Override
	public void treeNodesChanged(TreeModelEvent e){
		TreePath treePath = e.getTreePath();
		DefaultMutableTreeNode node = (DefaultMutableTreeNode)treePath.getLastPathComponent();//獲取樹枝節點
		System.out.println("node:"+node);
		try{
			int[] index = e.getChildIndices();
			System.out.println("枚舉子節點");
			for(int i=0;i<index.length;i++){
				System.out.println(index[i]);
			}
			node = (DefaultMutableTreeNode)node.getChildAt(index[0]);
			System.out.println("node:"+node);
		}catch(NullPointerException ne){
			label.setText("發生空指針異常");
		}
		label.setText(nodeName+"更改數據為:"+(String)node.getUserObject());
	}
	@Override
	public void treeNodesInserted(TreeModelEvent e){
		
	}
	@Override
	public void treeNodesRemoved(TreeModelEvent e){
		
	}
	@Override
	public void treeStructureChanged(TreeModelEvent e){
		
	}
	
	class MouseHandle extends MouseAdapter{
		@Override
		public void mousePressed(MouseEvent e){
			try{
				JTree tree = (JTree)e.getSource();
				int rowLocation = tree.getRowForLocation(e.getX(), e.getY());
				System.out.println("row:"+rowLocation);
				TreePath treePath = tree.getPathForRow(rowLocation);
				TreeNode treeNode = (TreeNode)treePath.getLastPathComponent();
				nodeName = treeNode.toString();
			}catch(NullPointerException ne){
				System.out.println("發生異常2");
			}
		}
	}
	
	public static void main(String...args){
		new JTreeDemo1();
	}
}



參考:深入淺出Java Swing程序設計
ps:用TreeModelEvent.getLastPathComponent取得DefaultMutableTreeNode節點為父節點,而不是用戶所選的節點,應該再加上node.getChildAt()來取得用戶真正所選的節點
但是通過JTree.getSelectionPath.getLastPathComponent()可以取得用戶所選的節點

你可能感兴趣的:(swing)