单击TreeView上的空白区域应自动选择root

时间:2015-12-28 13:04:39

标签: java treeview

我正在使用TreeView构建一个类系统。 Normaly根节点未显示。

我想要它,以便当我右键单击TreeView的空白区域(,即项目之间的)时,会自动选择根节点。我的目标是将根设置为不可见,并能够在根目录下添加新项目。

我试过这样的事情:

productTree.setOnMouseClicked(new EventHandler<MouseEvent>(){

        @Override
        public void handle(MouseEvent event) {

            if (productTree.getSelectionModel().getSelectedItem() == productTree.getRoot()){
                //productTree.getSelectionModel().clearSelection();
                if (productTree.getSelectionModel().isEmpty()){
                    productTree.getSelectionModel().select(productTree.getRoot());
                }
            }
            if (productTree.getSelectionModel().isEmpty()){
                    productTree.getSelectionModel().select(productTree.getRoot());
            }
        }

    });

但它不起作用。下面是我的一段代码。我希望有一个人可以帮助我。 image

productTree.setOnMouseClicked(new EventHandler<MouseEvent>(){

        @Override
        public void handle(MouseEvent event) {
            MouseButton button = event.getButton();
            if (MouseButton.SECONDARY == button){

                MyTreeItem selectedItem = (MyTreeItem) productTree.getSelectionModel().getSelectedItem();
                System.out.println("Secondary Mouse Button was pressed");
                ContextMenu context = new ContextMenu ();
                MenuItem newClass = new MenuItem ("New Class");
                MenuItem newProduct = new MenuItem ("new Product");
                MenuItem delete = new MenuItem ("Delete");
                context.getItems().addAll(newClass, newProduct, delete);
                productTree.setContextMenu(context);
                if(selectedItem.getType() == "class"){
                    newClass.setVisible(true);
                }
                else {
                    newClass.setVisible(false);
                    newProduct.setVisible(false);
                }
                newClass.setOnAction(new EventHandler<ActionEvent>(){
                    @Override
                    public void handle(ActionEvent event) {
                        Stage newClassStage = new Stage ();
                        AnchorPane pane = new AnchorPane();
                        newClassStage.setHeight(600);
                        newClassStage.setWidth(480);
                        Scene newClassScene = new Scene (pane);
                        newClassStage.setScene(newClassScene);
                        Label classNameLabel = new Label("Class Name");
                        TextField className = new TextField ();      

                        AnchorPane.setLeftAnchor(classNameLabel, 50.0);
                        AnchorPane.setTopAnchor(classNameLabel, 100.0);
                        AnchorPane.setLeftAnchor(className, 150.0);
                        AnchorPane.setTopAnchor(className, 100.0);
                        Button save = new Button ("Speichern");
                        Button cancel = new Button("Abbrechen");
                        AnchorPane.setLeftAnchor(cancel, 150.0);
                        AnchorPane.setTopAnchor(cancel, 200.0);
                        AnchorPane.setLeftAnchor(save, 250.0);
                        AnchorPane.setTopAnchor(save, 200.0);
                        pane.getChildren().addAll(classNameLabel, className, save, cancel);

                        newClassStage.show();
                        cancel.setOnAction(new EventHandler<ActionEvent>() {
                            @Override
                            public void handle(ActionEvent event) {
                              newClassStage.close();
                            }
                        });
                        save.setOnAction(new EventHandler<ActionEvent>(){
                            @Override
                            public void handle(ActionEvent event) {
                                MyTreeItem selectedItem = (MyTreeItem) productTree.getSelectionModel().getSelectedItem();
                                if(selectedItem.getType() != "product"){
                                    ImageView image = new ImageView(new Image(getClass().getResourceAsStream("Class.png")));
                                    int upperClassID = selectedItem.getClassID();
                                    String type =  selectedItem.getType();
                                    String name = selectedItem.getName();
                                    int hirarchieLevel = selectedItem.getHirarchieLevel() + 1;
                                    int classID = selectedItem.getClassID();
                                    boolean connected = DB.setClass(className.getText(), upperClassID, hirarchieLevel);
                                    if (connected == true){
                                        MyTreeItem newTreeItem = new MyTreeItem (className.getText(), classID, upperClassID, hirarchieLevel, "class", image);
                                        selectedItem.getChildren().add(newTreeItem);
                                        selectedItem.setID(DB.getclassID(className.getText())); 
                                        selectedItem.setID(classID);
                                        newClassStage.close();
                                    } else{
                                        System.out.println("No Database Connection!");
                                    }
                                }
                            }
                        });
                    }    
                });
                delete.setOnAction(new EventHandler<ActionEvent>(){
                    @Override
                    public void handle(ActionEvent event) {
                        boolean connected = false;
                        if (selectedItem.getType() == "product"){ 
                            connected = DB.deleteProduct(selectedItem.getClassID(), selectedItem.getType());
                            if (connected == true){
                                selectedItem.getParent().getChildren().remove(selectedItem);
                                connected = false;
                            } else {
                                System.out.println("No Database Connection!");
                            } 
                        } else if (selectedItem.getType() == "class"){
                            if (selectedItem.isLeaf() == true){
                                connected = DB.deleteProduct(selectedItem.getClassID(), selectedItem.getType());
                                if (connected == true){
                                    selectedItem.getParent().getChildren().remove(selectedItem);
                                    connected = false;
                                } else {
                                    System.out.println("No Database Connection!");
                                }
                            } else {
                                System.out.println("The Class has some Children!! Removing Tree Item is not possible. Please remove first the children");
                            }
                        }
                    }
                }); 
            }
        }
    });

2 个答案:

答案 0 :(得分:0)

你可以试试这个。

setOnMouseClicked(event -> {
        TreeCell<?> sourceCell = (TreeCell<?>) event.getSource();
        if (sourceCell.getItem() == null) {
            // you click on the empty area, do whatever you like
        }
    });

答案 1 :(得分:-1)

尝试使用TreeSelectionEvent而不是MouseEvent。如果未选择任何内容,则getLastSelectedPathComponent()方法应返回null。

https://docs.oracle.com/javase/tutorial/uiswing/components/tree.html#select