JavaFX拖放,鼠标图标旁边有自定义节点

JavaFX拖放,鼠标图标旁边有自定义节点,java,javafx,javafx-8,Java,Javafx,Javafx 8,在拖放过程中,在鼠标图标旁边显示节点半透明副本的最佳方式是什么 基本上,我有带彩色背景和文本标签的HBox,我想让它们看起来像是在被拖动时粘在鼠标光标上 如果用户能够直观地验证他们正在拖动什么,而不仅仅是看到鼠标光标变为各种拖动图标,那就好了。当您拖动某些组件(如RadioButton)时,场景生成器往往会执行此操作。节点的半透明副本是通过在节点上调用snapshotnull、null来完成的,这将返回一个可写图像。然后将此WritableImage设置为DragBoard的拖动视图。下面是一个

在拖放过程中,在鼠标图标旁边显示节点半透明副本的最佳方式是什么

基本上,我有带彩色背景和文本标签的HBox,我想让它们看起来像是在被拖动时粘在鼠标光标上


如果用户能够直观地验证他们正在拖动什么,而不仅仅是看到鼠标光标变为各种拖动图标,那就好了。当您拖动某些组件(如RadioButton)时,场景生成器往往会执行此操作。

节点的半透明副本是通过在节点上调用snapshotnull、null来完成的,这将返回一个可写图像。然后将此WritableImage设置为DragBoard的拖动视图。下面是一个关于如何做到这一点的小示例:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.input.ClipboardContent;
import javafx.scene.input.DataFormat;
import javafx.scene.input.Dragboard;
import javafx.scene.input.TransferMode;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class DragAndDrop extends Application {
    private static final DataFormat DRAGGABLE_HBOX_TYPE = new DataFormat("draggable-hbox");

    @Override
    public void start(Stage stage) {
        VBox content = new VBox(5);

        for (int i = 0; i < 10; i++) {
            Label label = new Label("Test drag");

            DraggableHBox box = new DraggableHBox();
            box.getChildren().add(label);

            content.getChildren().add(box);
        }

        stage.setScene(new Scene(content));
        stage.show();
    }

    class DraggableHBox extends HBox {
        public DraggableHBox() {
            this.setOnDragDetected(e -> {
                Dragboard db = this.startDragAndDrop(TransferMode.MOVE);

                // This is where the magic happens, you take a snapshot of the HBox.
                db.setDragView(this.snapshot(null, null));

                // The DragView wont be displayed unless we set the content of the dragboard as well. 
                // Here you probably want to do more meaningful stuff than adding an empty String to the content.
                ClipboardContent content = new ClipboardContent();
                content.put(DRAGGABLE_HBOX_TYPE, "");
                db.setContent(content);

                e.consume();
            });
        }
    }

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

节点的半透明副本是通过在节点上调用snapshotnull、null来完成的,它返回一个可写映像。然后将此WritableImage设置为DragBoard的拖动视图。下面是一个关于如何做到这一点的小示例:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.input.ClipboardContent;
import javafx.scene.input.DataFormat;
import javafx.scene.input.Dragboard;
import javafx.scene.input.TransferMode;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class DragAndDrop extends Application {
    private static final DataFormat DRAGGABLE_HBOX_TYPE = new DataFormat("draggable-hbox");

    @Override
    public void start(Stage stage) {
        VBox content = new VBox(5);

        for (int i = 0; i < 10; i++) {
            Label label = new Label("Test drag");

            DraggableHBox box = new DraggableHBox();
            box.getChildren().add(label);

            content.getChildren().add(box);
        }

        stage.setScene(new Scene(content));
        stage.show();
    }

    class DraggableHBox extends HBox {
        public DraggableHBox() {
            this.setOnDragDetected(e -> {
                Dragboard db = this.startDragAndDrop(TransferMode.MOVE);

                // This is where the magic happens, you take a snapshot of the HBox.
                db.setDragView(this.snapshot(null, null));

                // The DragView wont be displayed unless we set the content of the dragboard as well. 
                // Here you probably want to do more meaningful stuff than adding an empty String to the content.
                ClipboardContent content = new ClipboardContent();
                content.put(DRAGGABLE_HBOX_TYPE, "");
                db.setContent(content);

                e.consume();
            });
        }
    }

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

您是否正在执行平台支持的拖放操作,即在某个节点上调用startDragAndDrop?如果是这样,您可以调用Dragboard.setDragView。。。设置拖动的图像光标。只是内部,应用程序窗口外没有。这不是我要求的。我不清楚什么是支持平台。我认为这意味着在操作系统平台的桌面或文件夹中拖拽,而我没有这样做。正在其中一个HBox节点上调用startDragAndDrop,如下面的答案所示。有:简单按拖动释放默认值,完全按拖动释放通过调用startFullDrag启动,平台支持的拖放通过调用startDragAndDrop启动。下面概述的解决方案仅适用于第三个选项;但是,从你的问题中无法知道你正在做什么。你是否在做平台支持的拖放操作,即在某个节点上调用startDragAndDrop?如果是这样,您可以调用Dragboard.setDragView。。。设置拖动的图像光标。只是内部,应用程序窗口外没有。这不是我要求的。我不清楚什么是支持平台。我认为这意味着在操作系统平台的桌面或文件夹中拖拽,而我没有这样做。正在其中一个HBox节点上调用startDragAndDrop,如下面的答案所示。有:简单按拖动释放默认值,完全按拖动释放通过调用startFullDrag启动,平台支持的拖放通过调用startDragAndDrop启动。下面概述的解决方案仅适用于第三个选项;但是从你的问题中没有办法知道你在做什么。嘿,我不知道你可以传递null,null到快照,因为我没有传递足够的距离。。。啊,好吧,不管怎样,这是可行的。呵呵,我不知道你可以传递null,null到快照,因为我没有足够的RTFM。。。啊,好吧,不管怎样,这是有效的。