root : {
children : [{
text : '父叶',
children : [{
text : '子叶',
id : 'A',
leaf : true // 在你的叶节点添加 leaf 属性
}]
}]
}
})
// TreePanel 省略
treePanel.on('itemclick', function(view, record) {
if (record.get('leaf')) {
//你的逻辑
}
})
给叶节点设置监听的Java程序如下:package Tree.java
import java.awt.Container
import javax.swing.JFrame
import javax.swing.JScrollPane
import javax.swing.JTree
import javax.swing.event.TreeSelectionEvent
import javax.swing.event.TreeSelectionListener
import javax.swing.tree.DefaultMutableTreeNode
public class TreeWin extends JFrame implements TreeSelectionListener{
JTree tree
TreeWin()
{
setSize(300,250)
setVisible(true)
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
Container con = getContentPane()
DefaultMutableTreeNode root = new DefaultMutableTreeNode("根结点")
DefaultMutableTreeNode t1 = new DefaultMutableTreeNode("结点")
DefaultMutableTreeNode t2 = new DefaultMutableTreeNode("结点")
DefaultMutableTreeNode t1_1 = new DefaultMutableTreeNode("叶结点")
DefaultMutableTreeNode t1_2 = new DefaultMutableTreeNode("叶结点")
root.add(t1)
root.add(t2)
t1.add(t1_1)
t2.add(t1_2)
tree= new JTree(root) //创建根为root的树
tree.addTreeSelectionListener(this)//给整个树加选择监听
JScrollPane scrollPane = new JScrollPane(tree)
con.add(scrollPane)
}
public static void main(String[] args) {
TreeWin win = new TreeWin()
win.setVisible(true)
}
public void valueChanged(TreeSelectionEvent tse) {
DefaultMutableTreeNode selectionNode=(DefaultMutableTreeNode)tree.getLastSelectedPathComponent()
if(selectionNode.isLeaf()){ //叶子节点的监听
System.out.println(selectionNode.toString())
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)