将对象的n'Drop拖放到舞台外部的新窗口中

时间:2019-12-10 09:48:30

标签: java javafx drag-and-drop

使用JavaFX,我为DragEvent中的单元格设置了TreeView的处理程序,使其可以将其中包含的数据拖到应用程序内的其他组件中。 非常好,允许它也直接拖动Stage外部的单元格,并创建一个新窗口,即一个新的{{1} },以及与该拖动的单元格有关的数据。

但是,如果鼠标退出,将不再收到Stage 。我曾想过要创建一个透明的舞台,使其适合整个屏幕,并且可以处理所有拖动事件,并使鼠标也透明,但这似乎不起作用。

DragEvent

另一方面,通过设置Stage仍收到Pane root = new Pane(); root.setStyle("-fx-background-color: transparent"); Scene scene = new Scene(root, 1600, 1600); scene.setFill(Color.TRANSPARENT); root.prefWidthProperty().bind(scene.widthProperty()); root.prefHeightProperty().bind(scene.heightProperty()); // *** this won't work *** root.setOnDragOver(event -> Log.d("Drag over")); Stage stage = new Stage(StageStyle.TRANSPARENT); stage.setScene(scene); stage.show(); 。缺点是我必须有一个自定义的拖动处理程序,该处理程序充当中介并负责与拖动相关的数据以及拖动n'drop的状态。

有人知道更好的解决方案吗?或者我一无所知?


举一个最小的可复制示例:

MouseEvent

2 个答案:

答案 0 :(得分:0)

基于透明窗口的解决方案。

    import javafx.application.Application;
    import javafx.geometry.Rectangle2D;
    import javafx.scene.Scene;
    import javafx.scene.control.Label;
    import javafx.scene.input.ClipboardContent;
    import javafx.scene.input.Dragboard;
    import javafx.scene.input.TransferMode;
    import javafx.scene.layout.StackPane;
    import javafx.stage.Screen;
    import javafx.stage.Stage;
    import javafx.stage.StageStyle;

    public class DragAndDropApplicaton extends Application {

        public static void main(String[] args) {
            launch(args);
        }

        @Override
        public void start(Stage stage) throws Exception {
            StackPane dropStackPane = new StackPane();
            dropStackPane.setStyle("-fx-background-color: rgb(0, 0, 0, 0.01);");
            dropStackPane.setOnDragOver(event -> {
                if (event.getDragboard().hasString()) {
                    event.acceptTransferModes(TransferMode.COPY_OR_MOVE);
                }
                event.consume();
            });

            dropStackPane.setOnDragDropped(event -> {
                Stage newStage = new Stage();
                Scene newScene = new Scene(new StackPane(new Label("New stage")), 400, 400);
                newStage.setScene(newScene);
                newStage.show();

                event.setDropCompleted(true);
                event.consume();
            });

            Scene dropScene = new Scene(dropStackPane);
            dropScene.setFill(null);
            Stage dropStage = new Stage();
            dropStage.initStyle(StageStyle.TRANSPARENT);
            dropStage.setScene(dropScene);

            Rectangle2D visualBounds = Screen.getPrimary().getVisualBounds();
            dropStage.setX(visualBounds.getMinX());
            dropStage.setY(visualBounds.getMinY());
            dropStage.setWidth(visualBounds.getWidth());
            dropStage.setHeight(visualBounds.getHeight());
            dropStage.show();

            Label label = new Label("Drag me!");
            label.setOnDragDetected(event -> {
                Dragboard dragboard = label.startDragAndDrop(TransferMode.ANY);
                ClipboardContent clipboardContent = new ClipboardContent();
                clipboardContent.putString("label-drag");
                dragboard.setContent(clipboardContent);
                event.consume();
            });

            label.setStyle("-fx-background-color: red;");
            StackPane stackPane = new StackPane(label);

            Scene scene = new Scene(stackPane, 400, 400);
            stage.setScene(scene);
            stage.show();
        }
    }

答案 1 :(得分:0)

没有透明阶段的解决方案

//HourlyLocationsForecast/Location[@LocationMetaData/LocationName='JERUSALEM CENTRE']