E HelloDragAndDrop.java
有关描述,请参见实现基本的拖放手势。
法律条款和版权声明
/* * 版权所有 (c) 2011, 2014, Oracle及其附属公司。 * 保留所有权利。使用受许可条款约束。 * * 此文件可在以下许可下使用和许可: * * 源代码的重新发布和使用,无论是否进行修改,只要满足以下条件: * * - 必须保留上述版权声明、此条件列表和以下免责声明。 * - 以二进制形式重新发布的代码必须在文档和/或其他提供的材料中复制上述版权声明、此条件列表和以下免责声明。 * - 未经特定事先书面许可,不得使用Oracle或其贡献者的名称来认可或推广从本软件派生的产品。 * * 本软件由版权持有人和贡献者“按原样”提供,不提供任何明示或暗示的保证,包括但不限于适销性和适用性的暗示保证。 * 在任何情况下,版权所有者或贡献者均不对任何直接、间接、偶然、特殊、惩罚性或后果性损害(包括但不限于替代商品或服务的采购、使用损失、数据或利润损失、业务中断)承担任何责任,无论是合同责任、严格责任还是侵权行为(包括疏忽或其他)。 */
代码
package hellodraganddrop; import javafx.application.Application; import javafx.event.EventHandler; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.input.*; import javafx.scene.paint.Color; import javafx.scene.text.Text; import javafx.stage.Stage; /** * Demonstrates a drag-and-drop feature. */ public class HelloDragAndDrop extends Application { @Override public void start(Stage stage) { stage.setTitle("Hello Drag And Drop"); Group root = new Group(); Scene scene = new Scene(root, 400, 200); scene.setFill(Color.LIGHTGREEN); final Text source = new Text(50, 100, "DRAG ME"); source.setScaleX(2.0); source.setScaleY(2.0); final Text target = new Text(250, 100, "DROP HERE"); target.setScaleX(2.0); target.setScaleY(2.0); source.setOnDragDetected(new EventHandler <MouseEvent>() { @Override public void handle(MouseEvent event) { /* drag was detected, start drag-and-drop gesture*/ System.out.println("onDragDetected"); /* allow MOVE transfer mode */ Dragboard db = source.startDragAndDrop(TransferMode.MOVE); /* put a string on dragboard */ ClipboardContent content = new ClipboardContent(); content.putString(source.getText()); db.setContent(content); event.consume(); } }); target.setOnDragOver(new EventHandler <DragEvent>() { @Override public void handle(DragEvent event) { /* data is dragged over the target */ System.out.println("onDragOver"); /* accept it only if it is not dragged from the same node * and if it has a string data */ if (event.getGestureSource() != target && event.getDragboard().hasString()) { /* allow for moving */ event.acceptTransferModes(TransferMode.MOVE); } event.consume(); } }); target.setOnDragEntered(new EventHandler <DragEvent>() { @Override public void handle(DragEvent event) { /* the drag-and-drop gesture entered the target */ System.out.println("onDragEntered"); /* show to the user that it is an actual gesture target */ if (event.getGestureSource() != target && event.getDragboard().hasString()) { target.setFill(Color.GREEN); } event.consume(); } }); target.setOnDragExited(new EventHandler <DragEvent>() { @Override public void handle(DragEvent event) { /* mouse moved away, remove the graphical cues */ target.setFill(Color.BLACK); event.consume(); } }); target.setOnDragDropped(new EventHandler <DragEvent>() { @Override public void handle(DragEvent event) { /* data dropped */ System.out.println("onDragDropped"); /* if there is a string data on dragboard, read it and use it */ Dragboard db = event.getDragboard(); boolean success = false; if (db.hasString()) { target.setText(db.getString()); success = true; } /* let the source know whether the string was successfully * transferred and used */ event.setDropCompleted(success); event.consume(); } }); source.setOnDragDone(new EventHandler <DragEvent>() { @Override public void handle(DragEvent event) { /* the drag-and-drop gesture ended */ System.out.println("onDragDone"); /* if the data was successfully moved, clear it */ if (event.getTransferMode() == TransferMode.MOVE) { source.setText(""); } event.consume(); } }); root.getChildren().add(source); root.getChildren().add(target); stage.setScene(scene); stage.show(); } public static void main(String[] args) { Application.launch(args); } }