This discussion is archived
1 Reply Latest reply: Nov 22, 2012 5:48 AM by shakir.gusaroff RSS

Strange Selection in TreeView

975702 Newbie
Currently Being Moderated
Hello,

I am having some issues with TreeView. If attached code is run and the following tried

1. expand "RootNode"
2. expand "package B"
3. select either "B1" or "B2"
4. expand "package A"

Then there will be 2 tree items selected, either "B2" and "A3" or "B1" and "A2". The treeview has the selection mode set to single. Does anyone know what is causing this?


import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.control.TreeView;
import javafx.scene.control.TreeItem;
import javafx.scene.control.SelectionMode;

public class Main extends Application {
    
     public static void main(String[] args) {
        launch(args);
    }
    
    @Override
    public void start(Stage primaryStage) throws Exception{
          primaryStage.setTitle("TreeView Test");
          primaryStage.setScene(createScene());
          primaryStage.show();
    }
     
     private Scene createScene(){
          StackPane stackPane = new StackPane();
          TreeView treeView = new TreeView<String>();
          treeView.setRoot(createModel());
          treeView.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
          stackPane.getChildren().add(treeView);
          return new Scene(stackPane);
     }
     
     private TreeItem<String> createModel(){
          TreeItem<String> root = new TreeItem<String>("RootNode");
          TreeItem<String> packageA = new TreeItem<String>("package A");
          packageA.getChildren().addAll(new TreeItem<String>("A1"),new TreeItem<String>("A2"),new TreeItem<String>("A3"));
          TreeItem<String> packageB = new TreeItem<String>("package B");
          packageB.getChildren().addAll(new TreeItem<String>("B1"),new TreeItem<String>("B2"),new TreeItem<String>("B3"));
          root.getChildren().addAll(packageA,packageB);
          return root;
     }
}

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points