2014-06-20 78 views
0

我想開發一個簡單的例子,我可以選擇帶按鈕的TreeView節點(在某些情況下點擊圖像等)。我創建了這個簡單的示例:選擇樹節點上的按鈕點擊

package click.treenodeclicktest; 

import javafx.application.Application; 
import static javafx.application.Application.launch; 
import javafx.geometry.Insets; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.control.TreeItem; 
import javafx.scene.control.TreeView; 
import javafx.scene.layout.HBox; 
import javafx.stage.Stage; 

public class MainApp extends Application 
{ 
    @Override 
    public void start(Stage stage) throws Exception 
    { 
     HBox hb = new HBox(initTree(), initPanel()); 

     Scene scene = new Scene(hb, 800, 800); 

     stage.setTitle("JavaFX and Maven"); 
     stage.setScene(scene); 
     stage.show(); 
    } 

    public TreeView<String> initTree() 
    { 
     TreeItem<String> treeItemRoot = new TreeItem<>("Root"); 

     TreeItem<String> nodeItemA = new TreeItem<>("Item A"); 
     TreeItem<String> nodeItemB = new TreeItem<>("Item B"); 
     TreeItem<String> nodeItemC = new TreeItem<>("Item C"); 
     treeItemRoot.getChildren().addAll(nodeItemA, nodeItemB, nodeItemC); 

     TreeItem<String> nodeItemA1 = new TreeItem<>("Item A1"); 
     TreeItem<String> nodeItemA2 = new TreeItem<>("Item A2"); 
     TreeItem<String> nodeItemA3 = new TreeItem<>("Item A3"); 
     nodeItemA.getChildren().addAll(nodeItemA1, nodeItemA2, nodeItemA3); 

     TreeView<String> treeView = new TreeView<>(treeItemRoot); 

     return treeView; 
    } 

    public HBox initPanel() 
    { 
     Button bt1 = new Button("Item A1"); 
     Button bt2 = new Button("Item A2"); 
     Button bt3 = new Button("Item A3"); 

     HBox hb = new HBox(bt1, bt2, bt3); 
     hb.setPadding(new Insets(80, 80, 80, 80)); 

     return hb; 
    } 

    public static void main(String[] args) 
    { 
     launch(args); 
    } 

} 

問題是我如何選擇樹節點,當我點擊一個按鈕。例如,當我點擊一個按鈕Item A1我想選擇樹idem Item A1

回答

1

您的按鈕需要對treeView的引用以及對要選擇的對象的引用。所以你不知何故必須讓它們可用。然後,你可以叫:

bt1.setOnAction(new EventHandler<ActionEvent>() 
{ 
    @Override 
    public void handle(final ActionEvent event) 
    { 
    treeView.getSelectionModel().select(nodeItemA1); 
    } 
}); 

CNC中

我把你的代碼,並修改它,以滿足您的要求,但要記住,這可能是不希望有所有數據對象定義爲班級中的私人成員。它只是爲了說明它將如何工作來選擇一個節點:

public class MainApp extends Application 
{ 
    private TreeView<String> treeView; 

    private TreeItem<String> treeItemRoot; 
    private TreeItem<String> nodeItemA; 
    private TreeItem<String> nodeItemB; 
    private TreeItem<String> nodeItemC; 
    private TreeItem<String> nodeItemA1; 
    private TreeItem<String> nodeItemA2; 
    private TreeItem<String> nodeItemA3; 

    @Override 
    public void start(final Stage stage) throws Exception 
    { 
    HBox hb = new HBox(initTree(), initPanel()); 

    Scene scene = new Scene(hb, 800, 800); 

    stage.setTitle("JavaFX and Maven"); 
    stage.setScene(scene); 
    stage.show(); 
    } 

    public TreeView<String> initTree() 
    { 
    treeItemRoot = new TreeItem<>("Root"); 

    nodeItemA = new TreeItem<>("Item A"); 
    nodeItemB = new TreeItem<>("Item B"); 
    nodeItemC = new TreeItem<>("Item C"); 
    treeItemRoot.getChildren().addAll(nodeItemA, nodeItemB, nodeItemC); 

    nodeItemA1 = new TreeItem<>("Item A1"); 
    nodeItemA2 = new TreeItem<>("Item A2"); 
    nodeItemA3 = new TreeItem<>("Item A3"); 
    nodeItemA.getChildren().addAll(nodeItemA1, nodeItemA2, nodeItemA3); 

    treeView = new TreeView<>(treeItemRoot); 

    return treeView; 
    } 

    public HBox initPanel() 
    { 
    Button bt1 = new Button("Item A1"); 
    bt1.setOnAction(new EventHandler<ActionEvent>() 
    { 
     @Override 
     public void handle(final ActionEvent event) 
     { 
     treeView.getSelectionModel().select(nodeItemA1); 
     } 
    }); 

    Button bt2 = new Button("Item A2"); 
    bt2.setOnAction(new EventHandler<ActionEvent>() 
    { 
     @Override 
     public void handle(final ActionEvent event) 
     { 
     treeView.getSelectionModel().select(nodeItemA2); 
     } 
    }); 

    Button bt3 = new Button("Item A3"); 
    bt3.setOnAction(new EventHandler<ActionEvent>() 
    { 
     @Override 
     public void handle(final ActionEvent event) 
     { 
     treeView.getSelectionModel().select(nodeItemA3); 
     } 
    }); 

    HBox hb = new HBox(bt1, bt2, bt3); 
    hb.setPadding(new Insets(80, 80, 80, 80)); 

    return hb; 
    } 

    public static void main(final String[] args) 
    { 
    launch(args); 
    } 
} 
+0

你能告訴我一些工作的例子嗎? –

+0

使用您的代碼更新了答案以創建一個工作示例。 – crusam

+0

我可以嘗試從列表中選擇對象。有沒有辦法將樹項目作爲列表? –