package com.hengyuan.hyoas.page.test;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.event.TreeModelEvent;
import javax.swing.event.TreeModelListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
public class Tree implements ActionListener,TreeModelListener{
JLabel label=null;
JTree tree=null;
DefaultTreeModel treeModel=null;
String nodeName=null;//原有节点名称
public Tree(){
JFrame f=new JFrame("TreeTest");
Container contentPane=f.getContentPane();
DefaultMutableTreeNode root=new DefaultMutableTreeNode("菜单列表");
tree=new JTree(root);
tree.setEditable(true);
tree.addMouseListener(new MouseHandle());
treeModel=(DefaultTreeModel)tree.getModel();
treeModel.addTreeModelListener(this);
JScrollPane scrollPane=new JScrollPane();
scrollPane.setViewportView(tree);
JPanel panel=new JPanel();
JButton b=new JButton("新增节点");
b.addActionListener(this);
panel.add(b);
b=new JButton("删除节点");
b.addActionListener(this);
panel.add(b);
b=new JButton("清除所有节点");
b.addActionListener(this);
panel.add(b);
label=new JLabel("Action");
contentPane.add(panel,BorderLayout.NORTH);
contentPane.add(scrollPane,BorderLayout.CENTER);
contentPane.add(label,BorderLayout.SOUTH);
f.pack();
f.setVisible(true);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
//本方法运行新增、删除、清除所有节点的程序代码.
public void actionPerformed(ActionEvent ae){
if (ae.getActionCommand().equals("新增节点")){
DefaultMutableTreeNode parentNode=null;
DefaultMutableTreeNode newNode=new DefaultMutableTreeNode("新节点");
newNode.setAllowsChildren(true);
TreePath parentPath=tree.getSelectionPath();
//取得新节点的父节点
parentNode=(DefaultMutableTreeNode)(parentPath.getLastPathComponent());
//由DefaultTreeModel的insertNodeInto()方法增加新节点
treeModel.insertNodeInto(newNode,parentNode,parentNode.getChildCount());
//tree的scrollPathToVisible()方法在使Tree会自动展开文件夹以便显示所加入的新节点。若没加这行则加入的新节点
//会被 包在文件夹中,你必须自行展开文件夹才看得到。
tree.scrollPathToVisible(new TreePath(newNode.getPath()));
label.setText("新增节点成功");
}
if (ae.getActionCommand().equals("删除节点")){
TreePath treepath=tree.getSelectionPath();
if (treepath!=null){
//下面两行取得选取节点的父节点.
DefaultMutableTreeNode selectionNode=(DefaultMutableTreeNode)treepath.getLastPathComponent();
TreeNode parent=(TreeNode)selectionNode.getParent();
if (parent!=null) {
//由DefaultTreeModel的removeNodeFromParent()方法删除节点,包含它的子节点。
treeModel.removeNodeFromParent(selectionNode);
label.setText("删除节点成功");
}
}
}
if (ae.getActionCommand().equals("清除所有节点")){
//下面一行,由DefaultTreeModel的getRoot()方法取得根节点.
DefaultMutableTreeNode rootNode=(DefaultMutableTreeNode)treeModel.getRoot();
//下面一行删除所有子节点.
rootNode.removeAllChildren();
//删除完后务必运行DefaultTreeModel的reload()操作,整个Tree的节点才会真正被删除.
treeModel.reload();
label.setText("清除所有节点成功");
}
}
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 exc){}
label.setText(nodeName+"更改数据为:"+(String)node.getUserObject());
}
public void treeNodesInserted(TreeModelEvent e){
System.out.println("new node insered");
}
public void treeNodesRemoved(TreeModelEvent e){
System.out.println("node deleted");
}
public void treeStructureChanged(TreeModelEvent e){
System.out.println("Structrue changed");
}
public static void main(String[] args){
new Tree();
}
class MouseHandle extends MouseAdapter{
public void mousePressed(MouseEvent e){
try{
JTree tree=(JTree)e.getSource();
int rowLocation=tree.getRowForLocation(e.getX(),e.getY());
TreePath treepath=tree.getPathForRow(rowLocation);
TreeNode treenode=(TreeNode)treepath.getLastPathComponent();
nodeName=treenode.toString();
}catch(NullPointerException ne){}
}
}
}
分享到:
相关推荐
BinaryTree.java
java java_leetcode题解之Maximum Binary Tree.java
java java_leetcode题解之Distribute Coins in Binary Tree.java
java java_leetcode题解之Convert BST to Greater Tree.java
java java_leetcode题解之Construct String from Binary Tree.java
java java_leetcode题解之Closest Leaf in a Binary Tree.java
java基础 java_leetcode题解之Average of Levels in Binary Tree.java
java java_leetcode题解之Find Elements in a Contaminated Binary Tree.java
java java_leetcode题解之Lowest Common Ancestor of a Binary Tree.java
java基础 java_leetcode题解之All Nodes Distance K in Binary Tree.java
10.java Fig02_11.java Fig02_12.java Fig10_38.java Fig10_40.java Fig10_43.java Fig10_45.java Fig10_46.java Fig10_53.java Fig10_63.java HashFamily.java IntCell.java KdTree.java LeftistHeap.java ...
java代码实现平衡二叉树的增删查改
java面试算法/刷题
java面试算法/刷题
java 生成 Tree.htmjava 生成 Tree.htm
│ │ ShowTree.java │ │ USBSocket.java │ │ │ ├─view │ │ Foo.java │ │ HubNode.java │ │ KodakNode.java │ │ RioNode.java │ │ USBNode.java │ │ │ └─windows │ DeviceImpl.java │ JUSB....
这个实例结合了多个技术,包括Accordion布局、Servlet、Struts2以及JSON数据交互,以及Ext.tree.Panel组件,以创建一个功能丰富的用户界面。 Accordion布局是ExtJS中的一个布局管理器,它允许在一个区域内组织多个...
本文将深入探讨如何用Java语言实现数据库中的二叉树查找,以"BinaryTree.java"为例,帮助你理解相关知识。 二叉树是由节点构成的数据结构,每个节点包含一个值以及最多两个子节点,分别称为左子节点和右子节点。...
AVLTree.java文件应该包含了AVL树的实现,包括节点类的定义、插入、删除、旋转等核心功能。通过阅读和理解这个源代码,你可以深入学习AVL树的原理和操作,这对于理解和使用自平衡二叉查找树是非常有价值的。在实际...