JAVA之树(创建,遍历,修改,删除,标签,提示信息,展开折叠事件...)

JAVA之树(创建,遍历,修改,删除,标签,提示信息,展开折叠事件...)_第1张图片
JAVA之树(创建,遍历,修改,删除,标签,提示信息,展开折叠事件...)_第2张图片

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 map;
		public ToolTipNode(Map 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("" + map.get(value) + "");
			return renderer;
		}
		
	}
	
	public void cellRender(){
		ToolTipManager.sharedInstance().registerComponent(tree);
		Map map = new HashMap();
		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();
	}
}

你可能感兴趣的:(Java编程)