如何检测滑板的舞台元素?

时间:2019-08-03 17:33:56

标签: javafx

我正在使用Tabpane。我想使标签可分离。问题可能出在剪贴板上。

我试图在选项卡的图形上使用MouseRelease事件来检测其位置,但仅在删除拖动事件时才起作用:

graphic.setOnMouseReleased(e -> {
            Point2D mouseLoc = new Point2D(e.getScreenX(), e.getScreenY());
            Window window = tabPane.getScene().getWindow();
            Rectangle2D windowBounds = new Rectangle2D(window.getX(), window.getY(), window.getWidth(), window.getHeight());
            if(!windowBounds.contains(mouseLoc)){
                System.out.println("new stage");
            }
        });

我的拖动事件运行良好。我使用此方法将处理程序添加到选项卡:

public void addDragHandlers(Tab tab) {
        javafx.scene.control.Label label = new javafx.scene.control.Label(tab.getText(), tab.getGraphic());
        if (tab.getText() != null && !tab.getText().isEmpty()) {
            tab.setText(null);
            tab.setGraphic(label);
        }

        Node graphic = tab.getGraphic();
        graphic.setOnMouseReleased(e -> {
            Point2D mouseLoc = new Point2D(e.getScreenX(), e.getScreenY());
            Window window = tabPane.getScene().getWindow();
            Rectangle2D windowBounds = new Rectangle2D(window.getX(), window.getY(), window.getWidth(), window.getHeight());
            if(!windowBounds.contains(mouseLoc)){
                System.out.println("new stage");
            }
        });

        graphic.setOnDragDetected(e -> {
            Dragboard dragboard = graphic.startDragAndDrop(TransferMode.MOVE);
            ClipboardContent content = new ClipboardContent();
            content.putString(draggingID);
            dragboard.setContent(content);
            dragboard.setDragView(graphic.snapshot(null, null));
            currentDraggingTab = tab;
        });

        graphic.setOnDragOver(e -> {
            if (draggingID.equals(e.getDragboard().getString()) &&
                    currentDraggingTab != null &&
                    currentDraggingTab.getGraphic() != graphic) {
                e.acceptTransferModes(TransferMode.MOVE);
            }
        });

        graphic.setOnDragDropped(e -> {
            if (draggingID.equals(e.getDragboard().getString()) &&
                    currentDraggingTab != null &&
                    currentDraggingTab.getGraphic() != graphic) {
                int index = tab.getTabPane().getTabs().indexOf(tab);
                currentDraggingTab.getTabPane().getTabs().remove(currentDraggingTab);
                tab.getTabPane().getTabs().add(index, currentDraggingTab);
                currentDraggingTab.getTabPane().getSelectionModel().select(currentDraggingTab);
            }
        });

        graphic.setOnDragDone(e -> currentDraggingTab = null);
    }

如何在拖动处理程序中实现分离功能?

感谢您的帮助,对不起我的英语不好!

0 个答案:

没有答案