JavaFX TreeView handle selection event, get selection path

Description

JavaFX TreeView handle selection event, get selection path

// Demonstrate a TreeView 

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.MultipleSelectionModel;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;

public class Main extends Application {

   private Label response = new Label("No Selection");

   public static void main(String[] args) {
      launch(args);/*from w  ww.  j  a  v a2 s .  co m*/
   }

   public void start(Stage myStage) {
      myStage.setTitle("Demonstrate a TreeView");
      FlowPane rootNode = new FlowPane(10, 10);
      rootNode.setAlignment(Pos.CENTER);
      Scene myScene = new Scene(rootNode, 310, 460);
      myStage.setScene(myScene);

      // Create tree items, starting with the root.
      TreeItem<String> tiRoot = new TreeItem<String>("Root");

      // Now add subtrees, beginning with Level.
      TreeItem<String> tiLevel = new TreeItem<String>("Level");

      // Construct the Apple subtree.
      TreeItem<String> tiItem0 = new TreeItem<String>("Item0");
      tiItem0.getChildren().add(new TreeItem<String>("A"));
      tiItem0.getChildren().add(new TreeItem<String>("B"));
      tiItem0.getChildren().add(new TreeItem<String>("C"));

      tiLevel.getChildren().add(tiItem0);
      tiLevel.getChildren().add(new TreeItem<String>("Z1"));
      tiLevel.getChildren().add(new TreeItem<String>("A2"));

      // Finally, add the Level node to the root.
      tiRoot.getChildren().add(tiLevel);

      // Now, add Item3 subtree, using the same general process.
      TreeItem<String> tiItem3 = new TreeItem<String>("Item3");
      tiItem3.getChildren().add(new TreeItem<String>("A"));
      tiItem3.getChildren().add(new TreeItem<String>("B"));
      tiItem3.getChildren().add(new TreeItem<String>("C"));
      tiItem3.getChildren().add(new TreeItem<String>("D"));
      tiRoot.getChildren().add(tiItem3);

      // Likewise, add Item2 subtree.
      TreeItem<String> tiItem2 = new TreeItem<String>("Item2");
      tiItem2.getChildren().add(new TreeItem<String>("X"));
      tiItem2.getChildren().add(new TreeItem<String>("Y"));
      tiItem2.getChildren().add(new TreeItem<String>("Z"));
      tiRoot.getChildren().add(tiItem2);

      // Create tree view using the tree just created.
      TreeView<String> tvFood = new TreeView<String>(tiRoot);

      // Get the tree view selection model.
      MultipleSelectionModel<TreeItem<String>> tvSelModel = tvFood.getSelectionModel();

      // Use a change listener to respond to a selection within
      // a tree view
      tvSelModel.selectedItemProperty().addListener(new ChangeListener<TreeItem<String>>() {
         public void changed(ObservableValue<? extends TreeItem<String>> changed, TreeItem<String> oldVal,
               TreeItem<String> newVal) {
            // Display the selection and its complete path from the root.
            if (newVal != null) {

               // create the entire path to the selected item.
               String path = newVal.getValue();
               TreeItem<String> tmp = newVal.getParent();
               while (tmp != null) {
                  path = tmp.getValue() + " -> " + path;
                  tmp = tmp.getParent();
               }

               // Display the selection and the entire path.
               response.setText("Selection is " + newVal.getValue() + "\nComplete path is " + path);
            }
         }
      });

      rootNode.getChildren().addAll(tvFood, response);

      myStage.show();
   }
}



PreviousNext

Related