javafx 8 TreeView鼠标单击树的最后一个节点

时间:2015-11-26 16:22:51

标签: javafx treeview mouseevent

我有一个多级树视图。它有几个树项。我加载时只展开一个级别。用户可以通过单击该树项目的箭头逐个展开。随着我不断扩展,最后一个节点继续向下移动并离开视口。我向下滚动,然后单击箭头。我收到了错误。

    @FXML
    private void createDescendantsTreeView(TreeView<APerson> thisTreeView) {

        AnchorPane.setTopAnchor(thisTreeView, 10.0);
        AnchorPane.setBottomAnchor(thisTreeView, 10.0);
        AnchorPane.setLeftAnchor(thisTreeView, 5.0);
        AnchorPane.setRightAnchor(thisTreeView, 5.0);
        thisTreeView.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
        thisTreeView.setPrefHeight(vBoxHt);

        thisTreeView.setCellFactory(new Callback<TreeView<APerson>, TreeCell<APerson>>() {
            @Override
            public TreeCell<APerson> call(TreeView<APerson> thisTreeView) {
                return new TreeCell<APerson>() {
                    protected void updateItem(final APerson thisPerson, boolean empty) {

                        super.updateItem(thisPerson, empty);
                        // if the item is not empty and is a root...
                        if (!empty) {
                            setTooltip(thisPerson.getToolTip());
// This will set Context menu for the node
                            if (thisPerson.getFatherID() != 0) {
                                this.setContextMenu(rightContextMenu);
                            }

                            if (thisPerson.getGender().charAt(0) == 'M') {
                                setText(thisPerson.getPersonName() + " [" + thisPerson.getPersonID() + "]");
                                ImageView maleIcon = new ImageView(maleImage);
                                setGraphic(maleIcon);
                                String cellStyle = "    -fx-font-size: 16;\n" + "    -fx-font-family: \"Times New Roman\";\n" + "-fx-text-fill: blue;";
                                setStyle(cellStyle);
                            } else {
                                setText(thisPerson.getPersonName() + " [" + thisPerson.getPersonID() + "]");
                                ImageView femaleIcon = new ImageView(femaleImage);
                                setGraphic(femaleIcon);
                                String cellStyle = "    -fx-font-size: 16;\n" + "    -fx-font-family: \"Times New Roman\";\n" + "-fx-text-fill: red;";
                                setStyle(cellStyle);
                            }
                        } else {
                            this.setText(null);
                            this.setGraphic(null);
                        }
                    }
                };
            }
        });

        childrenTreeView.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent mouseEvent) {
                Node node = mouseEvent.getPickResult().getIntersectedNode();
                    System.out.println("Node click: " + node.toString());

                if (node instanceof Text || (node instanceof TreeCell && ((TreeCell) node).getText() != null)) {
                    TreeItem TreeItemAtLocation = new TreeItem();
                    TreeItemAtLocation = childrenTreeView.getSelectionModel().getSelectedItem();
                    System.out.println("Child Tree - clicked on : " + TreeItemAtLocation.getValue().toString());
                    String name = childrenTreeView.getSelectionModel().getSelectedItem().getValue().toString();
                    APerson tmp = childrenTreeView.getSelectionModel().getSelectedItem().getValue();
                    if (TreeItemAtLocation.getParent() == null) {
                        removePicture.setDisable(true);
                    } else {
                        removePicture.setDisable(false);
                    }
                    if (mouseEvent.getButton().equals(MouseButton.SECONDARY)) {
                        if (TreeItemAtLocation != null) {
                            if (childrenTreeView.getRoot() == TreeItemAtLocation) {
                                addPerson.setDisable(false);
                                deletePerson.setDisable(true);
                                deleteThisFamily.setDisable(true);
                                modifyPerson.setDisable(true);
                                viewPerson.setDisable(true);
                            } else {
                                addPerson.setDisable(false);
                                deletePerson.setDisable(false);
                                if (TreeItemAtLocation.isLeaf()) {
                                    deleteThisFamily.setDisable(true);
                                } else {
                                    deleteThisFamily.setDisable(false);
                                }
                                modifyPerson.setDisable(false);
                                viewPerson.setDisable(false);
                            }
                            rightContextMenu.show(myMainApp.getMainStage());
                        } else {
                            rightContextMenu.hide();
                        }
                    } else {
                        rightContextMenu.hide();
                    }
                }
            }
        });
    }

此图像显示屏幕,我正在谈论右侧的树。随着我不断扩展节点,最后一个Tree Item向下滚动,在此图像中不可见。我向下滚动以查看Tree Item,当我单击该Tree项的箭头时,我收到以下错误。

Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
Node click: MainWindowController$5$1@1051fb6[styleClass=cell indexed-cell tree-cell]'Daughter  [363]'
    at familytreev01.MainWindowController$6.handle(MainWindowController.java:510)
    at familytreev01.MainWindowController$6.handle(MainWindowController.java:501)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Scene$ClickGenerator.postProcess(Scene.java:3470)
    at javafx.scene.Scene$ClickGenerator.access$8100(Scene.java:3398)
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3766)
    at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:352)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:275)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$355(GlassViewEventHandler.java:388)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:387)
    at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
    at com.sun.glass.ui.View.notifyMouse(View.java:937)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$149(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:745)
Node click: MainWindowController$5$1@36f8bc[styleClass=cell indexed-cell tree-cell]'null'
Node click: StackPane@b679e7[styleClass=tree-disclosure-node]
Deleting directory D:\JavaFXPrograms\FamilyTreeV01\dist\run8821460
jfxsa-run:
BUILD SUCCESSFUL (total time: 16 minutes 1 second)

但是,如果我先选择Tree Item然后单击该箭头,我就不会收到该错误。它没有任何麻烦地扩展。右键单击该Tree Item也可以正常工作。

错误符合我将值赋给类变量的情况。 。childrenTreeView.getSelectionModel()getSelectedItem()的getValue();

为什么这只发生在最后一个节点?

提前致谢。

0 个答案:

没有答案