首页 > 代码库 > JTree单击事件
JTree单击事件
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单击事件
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。