Use*_*ser 1 java javafx javafx-8
在拖放期间在鼠标图标旁边显示节点的半透明“副本”的最佳方法是什么?
基本上,我有带有彩色背景和文本标签的 HBox,并且我想让它们在被拖动时“粘”到鼠标光标上。
如果用户可以直观地验证他们正在拖动的内容,而不是仅仅看到鼠标光标变成各种拖动图标,那就太好了。当您拖动某些组件(例如 RadioButton)时,Scene Builder 倾向于执行此操作。
节点的“半透明“复制”是通过调用节点来完成的snapshot(null, null),该节点返回一个WritableImage. 然后将其设置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();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1571 次 |
| 最近记录: |