package swing.tree;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
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 TreeEditFrame extends JFrame{
private JTree tree;
private DefaultTreeModel model;
private static int DEFAULT_WIDTH = 400;
private static int DEFAULT_HEIGHT = 200;
private boolean isCurrentOmuNormal = false;
public TreeEditFrame(){
super();
this.setTitle("Tree Edit Test");
this.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
TreeNode root = makeSampleTree();
model = new DefaultTreeModel(root);
model.setAsksAllowsChildren(true);//可以查询"允许子节点"
tree = new JTree(model);
tree.setEditable(true);
JScrollPane scrollPane = new JScrollPane(tree);
this.getContentPane().add(scrollPane, BorderLayout.CENTER);
this.makeButtons();
}
private void makeButtons() {
JPanel panel = new JPanel();
JButton addSibling = new JButton("Add Sibling");
JButton addChild = new JButton("Add Child");
JButton delete = new JButton("Delete");
panel.add(addSibling);
panel.add(addChild);
panel.add(delete);
this.getContentPane().add(panel, BorderLayout.SOUTH);
addSibling.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
DefaultMutableTreeNode selected = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
if(null==selected)return ;
DefaultMutableTreeNode parent = (DefaultMutableTreeNode)selected.getParent();
if(null==parent)return ;
int index = parent.getIndex(selected);
DefaultMutableTreeNode newNode = new DefaultMutableTreeNode("jinbo");
model.insertNodeInto(newNode, parent, index+1);
TreeNode [] nodes = model.getPathToRoot(newNode);
TreePath path = new TreePath(nodes);
tree.scrollPathToVisible(path);
}});
addChild.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
DefaultMutableTreeNode selected = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
if(null==selected)return ;
DefaultMutableTreeNode child = new DefaultMutableTreeNode("child");
model.insertNodeInto(child, selected, selected.getChildCount());
TreeNode [] nodes = model.getPathToRoot(child);
TreePath path = new TreePath(nodes);
tree.scrollPathToVisible(path);
}});
delete.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
DefaultMutableTreeNode selected = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
if(null != selected && null != selected.getParent()){
model.removeNodeFromParent(selected);
}
}
});
tree.addTreeSelectionListener(new TreeSelectionListener(){
public void valueChanged(TreeSelectionEvent e) {
TreePath path = tree.getSelectionPath();
if(null == path) return ;
DefaultMutableTreeNode node = (DefaultMutableTreeNode)path.getLastPathComponent();
if(null != node){
System.out.println(node.getUserObject());
}
}});
}
private TreeNode makeSampleTree() {
DefaultMutableTreeNode root = new DefaultMutableTreeNode("世界");
DefaultMutableTreeNode country_cn = new DefaultMutableTreeNode("中国");
DefaultMutableTreeNode country_usa = new DefaultMutableTreeNode("美国");
root.add(country_cn);
root.add(country_usa);
DefaultMutableTreeNode guangDong = new DefaultMutableTreeNode("广东");
DefaultMutableTreeNode huNan = new DefaultMutableTreeNode("湖南");
DefaultMutableTreeNode DC = new DefaultMutableTreeNode("华盛顿");
country_cn.add(guangDong);
country_cn.add(huNan);
country_usa.add(DC);
DefaultMutableTreeNode guangZhou = new DefaultMutableTreeNode("广州");
DefaultMutableTreeNode shenZhen = new DefaultMutableTreeNode("深圳");
DefaultMutableTreeNode changSha = new DefaultMutableTreeNode("长沙");
DefaultMutableTreeNode city3 = new DefaultMutableTreeNode("AAA");
guangDong.add(guangZhou);
guangDong.add(shenZhen);
huNan.add(changSha);
DC.add(city3);
guangZhou.setAllowsChildren(false);
shenZhen.setAllowsChildren(false);
return root;
}
class ClassNameTreeCellRenderer extends DefaultTreeCellRenderer{
@Override
public Component getTreeCellRendererComponent(JTree tree, Object value,
boolean sel, boolean expanded, boolean leaf, int row,
boolean hasFocus) {
super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
DefaultMutableTreeNode node = (DefaultMutableTreeNode)value;
if(!node.getAllowsChildren()){
this.setLeafIcon(null);//如果不允许有子节点,则该节点是叶子.
}else{
if(expanded){
if(isCurrentOmuNormal){
this.setOpenIcon(null);//设置该节点打开状态的图标(连接状态正常时为绿色)
}else{
this.setOpenIcon(null);//设置该节点打开状态的图标(连接状态异常时为红色)
}
}else{
this.setClosedIcon(null);//设置该节点关闭状态的图标
}
}
return null;
}
}
/**
* 撤消父子节点之间的连线:tree.putClientProperty("JTree.lineStyle","None");
* 控制根节点"手柄"的显示:tree.setShowRootHandles(true);
* 控制根节点是否可见:tree.setRootVisible(true);
* 获取树上选中的节点:DefaultMutableTreeNode node1 = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
* 设置树的选择模式:tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
* 设置一个节点是否允许有子节点:DefaultMutableTreeNode.setAllowsChildren(false);
* 以广度优先遍历节点:DefaultMutableTreeNode.breadthFirstEnumeration() 以深度优先遍历某节点:DefaultMutableTreeNode.depthFirstEnumeration()
* 查找树中的某一个节点:
* Enumeration e = root.breadthFirstEnumeration();
while(e.hasMoreElements()){
DefaultMutableTreeNode node = (DefaultMutableTreeNode)e.nextElement();
if(node.getUserObject().equals(obj)){
return node ;
}
}
设置树的节点图标
DefaultTreeCellRenderer ren = new DefaultTreeCellRenderer();
ren.setLeafIcon(null);
ren.setOpenIcon(null);
ren.setClosedIcon(null);
* 设置子节点允许查询:DefaultTreeModel.setAskAllowsChildren(boolean b);此方法主要用于叶子节点的显示,
* 如果b为true时,则DefaultMutableTreeNode.getAllowsChildren()返回false时,该节点
* 显示为叶子节点;如果b为flase,则DefaultMutableTreeNode.isLeaf()方法返回true时显示为叶子节点
* DefaultTreeModel.insertNodeInto():往树中增加一个节点
* DefaultTreeModel.removeNodeFromParent():从树中删除一个节点
*/
}