import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import javax.swing.DefaultCellEditor;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.ToolTipManager;
import javax.swing.WindowConstants;
import javax.swing.event.TreeExpansionEvent;
import javax.swing.event.TreeExpansionListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.event.TreeWillExpandListener;
import javax.swing.plaf.basic.BasicOptionPaneUI.ButtonActionListener;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.ExpandVetoException;
import javax.swing.tree.MutableTreeNode;
import javax.swing.tree.TreeCellEditor;
import javax.swing.tree.TreeCellRenderer;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;
import org.omg.CORBA.PRIVATE_MEMBER;
public class test extends JFrame{
private JTree tree;
DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
DefaultTreeModel model = new DefaultTreeModel(root, true);
JButton btnAdd = new JButton("添加");
JButton btnDel = new JButton("删除");
JButton btnFix = new JButton("修改");
DefaultMutableTreeNode[] rootF = new DefaultMutableTreeNode[3];
JPanel btnPanel = new JPanel();
JPanel btnCenter = new JPanel();
JTextField text;
public test(){
init();
addNode();
cellRender();
cellEdit();
}
private void cellEdit() {
// TODO Auto-generated method stub
text = new JTextField();
btnCenter.add(text);
TreeCellEditor cellE = new DefaultCellEditor(text);
tree.setEditable(true);
final JTextArea textArea = new JTextArea();
btnCenter.add(textArea);
tree.addTreeSelectionListener(new TreeSelectionListener() {
@Override
public void valueChanged(TreeSelectionEvent arg0) {
// TODO Auto-generated method stub
textArea.setText("");
TreePath path = tree.getSelectionPath();
DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
int count = node.getChildCount();
String info = "";
for(int i = 0;i<count;i++){
textArea.append(node.getChildAt(i).toString() + "\n");
}
}
});
}
private void addNode() {
// TODO Auto-generated method stub
btnAdd.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
MutableTreeNode nodeNew = new DefaultMutableTreeNode();
TreePath path = tree.getSelectionPath();
MutableTreeNode nodePar = (MutableTreeNode) path.getLastPathComponent();
int count = nodePar.getChildCount();
nodeNew.setUserObject("child"+(count+1));
model.insertNodeInto(nodeNew, nodePar, count);
TreePath NewPath = path.pathByAddingChild(nodeNew);
if(!tree.isVisible(NewPath)){
tree.makeVisible(NewPath);
}
}
});
btnFix.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
TreePath path = tree.getSelectionPath();
DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
node.setUserObject("fix");
model.nodeChanged(node);
tree.setSelectionPath(path);
repaint();
}
});
btnDel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
if(!node.isRoot()){
DefaultMutableTreeNode nodeNext = node.getNextSibling();
if(nodeNext == null){
nodeNext = (DefaultMutableTreeNode) node.getParent();
}
model.removeNodeFromParent(node);
tree.setSelectionPath(new TreePath(nodeNext.getPath()));
}
}
});
tree.addTreeWillExpandListener(new TreeWillExpandListener() {
@Override
public void treeWillExpand(TreeExpansionEvent arg0)
throws ExpandVetoException {
// TODO Auto-generated method stub
TreePath path = arg0.getPath();
DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
System.out.println("节点" + node + "将要被展开");
}
@Override
public void treeWillCollapse(TreeExpansionEvent arg0)
throws ExpandVetoException {
// TODO Auto-generated method stub
TreePath path = arg0.getPath();
DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
System.out.println("节点" + node + "将要被折叠");
}
});
tree.addTreeExpansionListener(new TreeExpansionListener() {
@Override
public void treeExpanded(TreeExpansionEvent arg0) {
// TODO Auto-generated method stub
TreePath path = arg0.getPath();
DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
System.out.println("节点" + node + "被展开");
}
@Override
public void treeCollapsed(TreeExpansionEvent arg0) {
// TODO Auto-generated method stub
TreePath path = arg0.getPath();
DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
System.out.println("节点" + node + "被折叠");
}
});
}
public void init(){
tree = new JTree(root);
JScrollPane js = new JScrollPane();
js.setViewportView(tree);
getContentPane().add(js,BorderLayout.CENTER);
tree.setShowsRootHandles(false);
for(int i = 0;i<3;i++){
rootF[i] = new DefaultMutableTreeNode(i+1);
DefaultMutableTreeNode[] rootC = new DefaultMutableTreeNode[2];
for(int j = 1;j<=2;j++){
rootC[j-1] = new DefaultMutableTreeNode("Child" + (i*2 + j));
rootF[i].add(rootC[j-1]);
}
root.add(rootF[i]);
}
tree.expandPath(new TreePath(root.getPath()));
DefaultTreeCellRenderer render = (DefaultTreeCellRenderer) tree.getCellRenderer();
//ImageIcon icon = new ImageIcon("ii.jpg");
render.setLeafIcon(null);
render.setClosedIcon(null);
render.setOpenIcon(null);
btnCenter.setLayout(new GridLayout(5,1,0,0));
btnPanel.setLayout(new GridLayout(4, 1,0,0));
JButton[] btn = new JButton[4];
String[] opeName = {"先序","后序","bfs","dfs"};
for(int i = 1;i<=4;i++){
btn[i-1] = new JButton(opeName[i-1]);
btnPanel.add(btn[i-1]);
}
btnCenter.add(btnAdd);
btnCenter.add(btnFix);
btnCenter.add(btnDel);
for(int i = 0;i<4;i++){
btn[i].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
Enumeration enumeration = null;
String btnName = arg0.getActionCommand();
if(btnName.equals("先序")){
enumeration = root.preorderEnumeration();
}else{
if(btnName.equals("后序")){
enumeration = root.postorderEnumeration();
}else{
if(btnName.equals("bfs")){
enumeration = root.breadthFirstEnumeration();
}else{
if(btnName.equals("dfs"))
enumeration = root.depthFirstEnumeration();
}
}
}
while(enumeration.hasMoreElements()){
DefaultMutableTreeNode node = (DefaultMutableTreeNode)enumeration.nextElement();
for(int j = 0;j<node.getLevel();j++){
System.out.print("---");
}
System.out.println(node.getUserObject());
}
}
});
}
getContentPane().add(btnPanel,BorderLayout.EAST);
getContentPane().add(btnCenter,BorderLayout.WEST);
setVisible(true);
setSize(300, 300);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public class ToolTipNode implements TreeCellRenderer{
private DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer();
private Map<DefaultMutableTreeNode,String> map;
public ToolTipNode(Map<DefaultMutableTreeNode,String> map){
this.map = map;
}
@Override
public Component getTreeCellRendererComponent(JTree tree, Object value,
boolean selected, boolean expanded, boolean leaf, int row,
boolean hasFocus) {
// TODO Auto-generated method stub
renderer.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
renderer.setToolTipText("<html><font face=黑体 size=16 color=red>" + map.get(value) + "</font></html>");
return renderer;
}
}
public void cellRender(){
ToolTipManager.sharedInstance().registerComponent(tree);
Map<DefaultMutableTreeNode,String> map = new HashMap<DefaultMutableTreeNode,String>();
map.put(root,"有"+root.getChildCount()+"个子文件");
for(int i = 0;i<3;i++){
map.put(rootF[i], "有"+rootF[i].getChildCount()+"个子文件");
}
tree.setCellRenderer(new ToolTipNode(map));
}
public static void main(String[] agrs){
new test();
}
}