2012-11-01 67 views
0

這裏是我的JTree獲得選擇的節點JTree的java的

javax.swing.tree.DefaultMutableTreeNode treeNode1 = new javax.swing.tree.DefaultMutableTreeNode(".Net Framework"); 
javax.swing.tree.DefaultMutableTreeNode treeNode2 = new javax.swing.tree.DefaultMutableTreeNode("Install"); 
javax.swing.tree.DefaultMutableTreeNode treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("V1"); 
treeNode2.add(treeNode3); 

treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("V2"); 
treeNode2.add(treeNode3); 

treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("V3"); 
treeNode2.add(treeNode3); 

treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("V4"); 
treeNode2.add(treeNode3); 
treeNode1.add(treeNode2); 

treeNode2 = new javax.swing.tree.DefaultMutableTreeNode("Repair"); 
treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("V1"); 
treeNode2.add(treeNode3); 

treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("V2"); 
treeNode2.add(treeNode3); 

treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("V3"); 
treeNode2.add(treeNode3); 

treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("V4"); 
treeNode2.add(treeNode3); 
treeNode1.add(treeNode2); 

treeNode2 = new javax.swing.tree.DefaultMutableTreeNode("Removal"); 
treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("Remove All"); 
treeNode2.add(treeNode3); 
treeNode1.add(treeNode2); 

jTree2.setModel(new javax.swing.tree.DefaultTreeModel(treeNode1)); 
jScrollPane5.setViewportView(jTree2); 

這裏是幾乎工程

DefaultMutableTreeNode node = (DefaultMutableTreeNode) jTree2.getLastSelectedPathComponent(); 

if (node == null) { 
     //Nothing is selected.  
    return; 
} 

if (node.isLeaf()) { 
    System.out.println(node); 
} 

我希望能夠讓代碼,如果語句說,如果在葉節點V1安裝節點,然後執行此操作。只是不知道if(node.equals(thisleaf)))怎麼辦,什麼thisleaf對象應該等於。

回答

0

聲明一個場

private DefaultMutableTreeNode v1Node; 

更改第三行

this.v1Node = new DefaultMutableTreeNode("V1"); 

,並使用

if (node.equals(this.v1Node)) 

旁註:你應該導入javax.swing.tree.DefaultMutableTreeNode,使你的代碼更易讀,只需使用DefaultMutableTreeNode而不是完全限定的名稱。您還應該選擇有意義的變量名稱而不是treeNode1,jTree2等。這些糟糕的名稱使代碼非常難以理解。