6 Replies Latest reply: Jun 20, 2014 6:04 PM by James_D RSS

    Select Tree Node on button click

    2644621

      I want to develop simple example in which I can select TreeView Nodes with Buttons(in some cases clicking on image and etc). I created this simple sample:

       

      <pre style="" _modifiedtitle="true" jivemacrouid="_14032555963537721" ___default_attr="java" jivemacro="code" class="jive_text_macro jive_macro_code jivemacrouid_14032555963537721">

      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);

          }

       

      }

       

      <pre style="" _modifiedtitle="true" jivemacrouid="_14032555963537721" ___default_attr="java" jivemacro="code" class="jive_text_macro jive_macro_code jivemacrouid_14032555963537721">

       

       

      The question is how I can select tree Node when I click on a button. For example when I click on a button Item A1 I want to select tree idem Item A1.

       

      Ref

      javafx - Select Tree Node on button click - Stack Overflow

       

       

        • 1. Re: Select Tree Node on button click
          2644621

          Any help please?

          • 2. Re: Select Tree Node on button click
            David Grieve

            You just need to call TreeView#edit(TreeItem) API

            • 3. Re: Select Tree Node on button click
              2644621

              Can you show me some example, please?

              • 4. Re: Select Tree Node on button click
                David Grieve

                You can find an example in the javadoc for TreeView, here TreeView (JavaFX 8)

                • 5. Re: Select Tree Node on button click
                  2644621

                  This is the example that I developed. Unfortunately I I cannot select sub nodes. Is there any better approach?

                   

                  public class MainApp extends Application
                  {
                      HashMap<Integer, TreeItem<String>> cache = new HashMap<>();
                  
                      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);
                  
                          int pp = treeItemRoot.getChildren().size();
                  
                          for (int i = 0; i < pp; i++)
                          {
                              TreeItem<String> ddd = treeItemRoot.getChildren().get(i);
                              cache.put(i, ddd);
                          }
                  
                          for (int j = 0; j < 10; j++)
                          {
                              System.out.println(cache.get(j));
                          }
                  
                          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(cache.get(0));
                              }
                          });
                  
                          Button bt2 = new Button("Item A2");
                          bt2.setOnAction(new EventHandler<ActionEvent>()
                          {
                              @Override
                              public void handle(final ActionEvent event)
                              {
                                  treeView.getSelectionModel().select(cache.get(1));
                              }
                          });
                  
                          Button bt3 = new Button("Item A3");
                          bt3.setOnAction(new EventHandler<ActionEvent>()
                          {
                              @Override
                              public void handle(final ActionEvent event)
                              {
                                  treeView.getSelectionModel().select(cache.get(2));
                              }
                          });
                  
                          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);
                      }
                  }