码迷,mamicode.com
首页 > 其他好文 > 详细

JTree单击事件

时间:2017-01-11 11:59:33      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:win   ddt   eem   end   awt   log   ble   demo   event   

import javax.swing.*;
import javax.swing.tree.*;
import java.awt.FlowLayout;
import java.awt.GridLayout;
public class TreeDemo extends JFrame{
 JTree jTree=new JTree();
 JTextField jTextField =new JTextField();
 GridLayout lay=new GridLayout(1,1);
 public TreeDemo(){
  treeinit();
  this.setSize(400,400);
  this.setLayout(lay);
  this.add(jTree);
  this.add(jTextField);
  this.setVisible(true);
  jTree.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
            public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {
                jTreeValueChanged(evt);
            }
        });
 }
  private void jTreeValueChanged(javax.swing.event.TreeSelectionEvent evt) {
        DefaultMutableTreeNode selectedNode=(DefaultMutableTreeNode) jTree.getLastSelectedPathComponent();//返回最后选定的节点
        if(selectedNode.toString().equals("child")){
         jTextField.setText("I LOVE YOU");
        }

}
 public void treeinit(){
  DefaultMutableTreeNode root=new DefaultMutableTreeNode("root");
  DefaultMutableTreeNode father=new DefaultMutableTreeNode("father");
        DefaultMutableTreeNode child=new DefaultMutableTreeNode("child");
        father.add(child);
        root.add(father);
        TreeModel treeModel = new DefaultTreeModel(root);
        jTree.setModel(treeModel);
 }
 public static void main(String args[]){
  new TreeDemo();
 }
}

  

JTree单击事件

标签:win   ddt   eem   end   awt   log   ble   demo   event   

原文地址:http://www.cnblogs.com/nicebaby/p/6273001.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!