user1119859
user1119859

Reputation: 719

Vaadin 24 Drag and Drop

I'm trying to get drag'n drop working with Vaadin 24. Should be an easy thing. But somehow, it simply does not work. This is my code:

public DashboardView() {

        System.out.println("View loaded!");

        // Drag-Quelle
        Div dragSourceDiv = new Div(new Text("Drag me!"));
        dragSourceDiv.getStyle()
                .set("padding", "20px")
                .set("background-color", "lightgray")
                .set("cursor", "grab");
        DragSource<Div> dragSource = DragSource.create(dragSourceDiv);
        dragSource.setEffectAllowed(EffectAllowed.MOVE);
        dragSource.addDragStartListener(event -> System.out.println("Drag started!"));

        // Drop-Ziel
        Div dropTargetDiv = new Div();
        dropTargetDiv.setWidth("300px");
        dropTargetDiv.setHeight("300px");
        dropTargetDiv.getStyle()
                .set("border", "3px dashed blue")
                .set("background-color", "yellow");
        DropTarget<Div> dropTarget = DropTarget.create(dropTargetDiv);
        dropTarget.setDropEffect(DropEffect.MOVE);
        dropTarget.addDropListener(event -> {
            System.out.println("Drop-Event!");
            event.getDragSourceComponent().ifPresent(source -> {
                System.out.println("Widget dropped: " + source.getElement().getText());
            });
        });

        add(dragSourceDiv, dropTargetDiv);
    }

Unfortunately, it does not work. "Drop-Event!" is never printed on screen. What am I doing wrong?

thanks, Thorsten

Upvotes: 1

Views: 62

Answers (0)

Related Questions