setOnDragDropped
Pavel Safrata
pavel.safrata at oracle.com
Fri Oct 19 13:41:02 PDT 2012
Hello John,
your drop target node has to tell to the system that it is a drop
target. In the OnDragOver handler you need to do something like
| Dragboard db = event.getDragboard();
if (db.hasString()) {
event.acceptTransferModes(TransferMode.COPY_OR_MOVE);
}
|
The acceptTransferModes call tells to the system that your node is a
potential drop target for the dragged data. This enables the drop event
for your node (and also, at least on Windows, switches the cursor from
the "stop" sign to the "copy", "move" or "link" sign).
Please refer to the DragEvent's javadoc, the proper usage is described
there, including example code snippets.
Regards,
Pavel
On 19.10.2012 20:51, John McDonnell wrote:
> Any change someone can point out the errors of my ways, when trying to drop
> a node on another one?
>
>
> I have simplified my code so I can show you a sample of my problem[1]. The
> sample shows 2 labels. One that can be dragged, and another that is just
> there to allow the dragged label to be dropped on.
>
> In my sample when I drag the draggable node it works as expected, I even
> can detected that the dragged label is dragged over the other label
> listening on the onDragOver property of the second label.
>
> But when ever I try to attach a listener to either label to detect when the
> label has been dropped I am not getting any thing printed in the console.
> Should the DragDropped event listener be added to the draggable label?
> should the DragDropped event be fired when the mouse button is release of
> the dragged label right?
>
> [1] Sample code:
> public class DropTests extends Application
> {
> @Override
> public void start(Stage primaryStage)
> {
> final HBox root = new HBox();
> root.setSpacing(25.0d);
>
> Label lblDraggable = new Label();
> lblDraggable.setText("'Drag me'");
> lblDraggable.setOnDragDetected(new EventHandler<MouseEvent>()
> {
> @Override
> public void handle(MouseEvent event)
> {
> System.out.println("Drag Detected");
> Dragboard db = root.startDragAndDrop(TransferMode.ANY);
>
> /* Put a string on a dragboard */
> ClipboardContent content = new ClipboardContent();
> content.putString("Label Being Dragged.");
> db.setContent(content);
>
> event.consume();
> }
> });
>
> Label lblDragOver = new Label();
>
> lblDragOver.setText("Drag over Me");
> lblDragOver.setOnDragOver(new EventHandler<DragEvent>() {
>
> @Override
> public void handle(DragEvent t)
> {
> System.out.println("Drag Over.");
> t.consume();
> }
> });
>
> lblDragOver.setOnDragDropped(new EventHandler<DragEvent>() {
>
> @Override
> public void handle(DragEvent t)
> {
> System.out.println("Drag DROPPED");
> t.consume();
> }
> });
>
> root.getChildren().add(lblDragOver);
> root.getChildren().add(lblDraggable);
> Scene scene = new Scene(root, 300, 250);
>
> primaryStage.setTitle("Dragging Tests");
> primaryStage.setScene(scene);
> primaryStage.show();
> }
>
More information about the openjfx-dev
mailing list