package test;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
public class MyTree extends JFrame implements TreeSelectionListener,ActionListener{
private JTree jt;
private DefaultTreeModel model;
private DefaultTreeCellRenderer render;
private TreeNode node;
private JButton add_sib,add_child,delete,update;
public MyTree(){
model=new DefaultTreeModel(getNode());
jt=new JTree(model);
jt.addTreeSelectionListener(this);
JScrollPane jsc=new JScrollPane(jt);
add_sib=new JButton("添加同级节点");
add_child=new JButton("添加子节点");
delete=new JButton("删除");
update=new JButton("编辑");
add_sib.addActionListener(this);
add_child.addActionListener(this);
delete.addActionListener(this);
update.addActionListener(this);
Box box=Box.createHorizontalBox();
box.add(add_sib);
box.add(add_child);
box.add(delete);
box.add(update);
add(jsc,BorderLayout.CENTER);
add(box,BorderLayout.SOUTH);
this.pack();
this.setResizable(true);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public TreeNode getNode(){
DefaultMutableTreeNode root=new DefaultMutableTreeNode("中国");
DefaultMutableTreeNode province=new DefaultMutableTreeNode("广东");
DefaultMutableTreeNode city=new DefaultMutableTreeNode("深圳");
root.add(province);
province.add(city);
DefaultMutableTreeNode area=new DefaultMutableTreeNode("福田区");
city.add(area);
area=new DefaultMutableTreeNode("保安区");
city.add(area);
city=new DefaultMutableTreeNode("广州");
province.add(city);
province=new DefaultMutableTreeNode("江西");
city=new DefaultMutableTreeNode("南昌");
root.add(province);
province.add(city);
area=new DefaultMutableTreeNode("青山湖区");
city.add(area);
area=new DefaultMutableTreeNode("高新区");
city.add(area);
city=new DefaultMutableTreeNode("九江");
province.add(city);
return root;
}
public static void main(String[] args) {
MyTree mt=new MyTree();
}
@Override
public void valueChanged(TreeSelectionEvent e) {
// System.out.println(jt.getLastSelectedPathComponent().toString());
}
@Override
public void actionPerformed(ActionEvent e) {
DefaultMutableTreeNode newNode = new DefaultMutableTreeNode("New");
DefaultMutableTreeNode selectNode=(DefaultMutableTreeNode)jt.getLastSelectedPathComponent();
if(e.getSource().equals(add_sib)){
DefaultMutableTreeNode parent=(DefaultMutableTreeNode)selectNode.getParent();
if(parent!=null){
int index=parent.getIndex(selectNode);
model.insertNodeInto(newNode, parent, index+1);
}
}else if(e.getSource().equals(add_child)){
int count=selectNode.getChildCount();
model.insertNodeInto(newNode, selectNode, count);
}
TreeNode[] nodes = model.getPathToRoot(newNode);
TreePath path = new TreePath(nodes);
jt.scrollPathToVisible(path);
}
}