Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX OnDragDropped Not Registering

I am trying to drag and drop labels on top of each other.

I have a list of labels called starsAndBars, and every label has this called on it as it is created:

private void giveDragAndDropProperties(Label label) {
    //Enable drag actions to pick up the label
    label.setOnDragDetected(new EventHandler<MouseEvent>() {
        public void handle(MouseEvent event) {
            System.out.println("Drag and drop started!");

            Dragboard db = label.startDragAndDrop(TransferMode.ANY);
            ClipboardContent content = new ClipboardContent();
            int index = starsAndBars.indexOf(label);
            content.putString("test");
            db.setContent(content);

            event.consume();
        }
    });

    label.setOnDragEntered(new EventHandler<DragEvent>() {
        public void handle(DragEvent event) {
            System.out.println("drag entered!");
        }
    });

    label.setOnDragExited(new EventHandler<DragEvent>() {
        public void handle(DragEvent event) {
            System.out.println("drag left!");
        }
    });

    //Enable a label to be dropped on this label
    label.setOnDragDropped(new EventHandler<DragEvent>() {
        public void handle(DragEvent event) {
            System.out.println("Drop occurred!");
        }
    });
}

When I attempt to perform a drag and drop operation, this is what I see:

Drag and drop started!
drag entered!
drag left!
drag entered!
<I release the mouse here>
drag left!

The "onDragDropped" event never registers, as far as I can tell. What am I doing wrong? I can tell that each Label has the drag and drop event handlers since they register the "entered" and "exited" events. This is the only piece of code that deals with Drag and Drop event handlers - so it should be being consumed by something else first.

All of these labels are in an HBox that is within a ScrollPane, if that makes any difference.

Edit: I have now tried putting a drop event on the HBox the labels are contained in with the same result - it never seems to get called. My code for that setOnDragDropped was essentially identical to the one in my other example.

I have tried rewriting both my setOnDragDetected and setOnDragDropped, and I am trying to match them as closely to this example (http://docs.oracle.com/javafx/2/drag_drop/jfxpub-drag_drop.htm) as possible, which has worked for me before.

Edit 2: I have another project that currently has drag and drop events working with events very nearly identical to this, though with different objects - I have tried copying source from that project (and changing the variable names) with no luck. I can include that source as well, if it might help. But it is essentially the same.

What more can I include to help reach an answer?

like image 581
Porthos3 Avatar asked Jul 14 '14 00:07

Porthos3


1 Answers

You need to accept the transfer mode in setOnDragOver, like this:

label.setOnDragOver(new EventHandler <DragEvent>() {

    public void handle(DragEvent event) {
        event.acceptTransferModes(TransferMode.ANY);
        event.consume();
    }
});
like image 76
AlianaWolf Avatar answered Oct 06 '22 01:10

AlianaWolf