JTree的事件监听

类库:
import javax.swing.tree.*;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;

使用流程:
1,JTree tree = createTree();//得到JTree对象,createTree()见文后

2,DefaultTreeCellRenderer render = new DefaultTreeCellRenderer();
  tagTree.setCellRenderer( render );//设置Tree的Render模式

3,thdTree.addTreeSelectionListener(
   new TreeSelectionListener()
   {
      public void valueChanged(TreeSelectionEvent e){

      }
   }
  );//增加选中节点的监听器

private void createTree()
{
    //一般构造方法
     JTree tree;
     DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
     DefaultTreeModel treeModel=new DefaultTreeModel(root);       
     root.setAllowsChildren(true);
     tree = new JTree(treeModel);

     DefaultTreeCellRenderer treeRender = new DefaultTreeCellRenderer();
     treeRender.setOpaque(true);
     tree.setCellRenderer(treeRender);
     ToolTipManager.sharedInstance().registerComponent(tree);
     tree.setName("tagTree");     
     return tree;
}

你可能感兴趣的:(swing)