You are viewing documentation for Vaadin Framework 8 and related products View documentation for Vaadin Framework 7 ›
Drag and Drop · Vaadin
Vaadin Framework - Advanced Topics - Drag and Drop
 Edit This Page

Drag and Drop

This feature is currently being developed and only available in the Framework 8.1 prerelease versions, starting from 8.1.0.alpha1.

Dragging an object from one location to another by grabbing it with mouse, holding the mouse button pressed, and then releasing the button to "drop" it to the other location is a common way to move, copy, or associate objects. For example, most operating systems allow dragging and dropping files between folders or dragging a document on a program to open it. Framework version 8.1 adds support for HTML5 drag and drop features. This makes it possible to set components as drag sources that user can drag and drop, or to set them as drop targets to drop things on.

Drag Source

Any component can be made a drag source that has a set of data that is transferred when it is dragged and dropped.

To make a component a drag source, you apply the DragSourceExtension to it. Then you can define the data to transfer, and the allowed drag effect.

Label draggableLabel = new Label("You can grab and drag me");
DragSourceExtension<Label> dragSource = new DragSourceExtension<>(draggableLabel);

// set the allowed effect
dragSource.setEffectAllowed(EffectAllowed.MOVE);
// set the data to transfer
dragSource.setTransferData("text/plain", "hello receiver");

The effect allowed specifies the allowed effects that must match the drop effect of the drop target. If these don’t match, the drop event is never fired on the target. If multiple effects are allowed, the user can use the modifier keys to switch between the desired effects. The default effect and the modifier keys are system and browser dependent.

The transfer data is a set of data, that the drop target will receive in the drop event. The first parameter given is the type of the data, and the second parameter is the actual data as string. The type parameter thus acts as a key for the actual data. For more information about the type parameter, see MDN recommendations.

The DragStartEvent is fired when the drag has started, and the DragEndEvent event when the drag has ended, either in a drop or a cancel.

dragSource.addDragStartListener(event ->
    event.getComponent().addStyleName("dragged")
);
dragSource.addDragEndListener(event ->
    event.getComponent().removeStyleName("dragged")
);

The browsers allow the user to select and drag and drop text, which may cause some issues when trying to drag a component that has text. You can fix this by setting the following style rule to the drag source component to prevent dragging of the text instead of the whole component.

user-select: none;

Drop Target

The drag operations end when the mouse button is released on a valid drop target. It is then up to the target to react to the drop event and the data associated with the drag, set by the drag source.

To make a component be a drop target, you apply the DropTargetExtension to it. The extension allows you to control when the drop is acceptable and then react to the drop event.

VerticalLayout dropTargetLayout = new VerticalLayout();
dropTargetLayout.setCaption("Drop things inside me");
dropTargetLayout.addStyleName(ValoTheme.LAYOUT_CARD);

// make the layout accept drops
DropTargetExtension<VerticalLayout> dropTarget = new DropTargetExtension<>(dropTargetLayout);

// the drop effect must match the allowed effect in the drag source for a successful drop
dropTarget.setDropEffect(DropEffect.MOVE);

// catch the drops
dropTarget.addDropListener(event -> {
    // if the drag source is in the same UI as the target
    Optional<AbstractComponent> dragSource = event.getDragSourceComponent();
    if (dragSource.isPresent() && dragSource.get() instanceof Label) {
        // move the label to the layout
        dropTargetLayout.addComponent(dragSource.get());

        // get possible transfer data
        // NOTE that "text" is same as "text/plain" from drag source data,
        // see the HTML5 standard for more info
        String message = event.getTransferData("text");
        Notification.show("DropEvent with data transfer: "+ message);
    }
});

When data is dragged over a drop target, the v-drag-over class name is applied to the root element of the drop target component automatically.

Controlling When The Drop is Acceptable

The drop effect allows you to specify the desired drop effect, and for a succesful drop it must match the allowed effect that has been set for the drag source. Note that you can allow multiple effects, and that you should not rely on the default effect since it may vary between browsers.

The drag over criteria allows you determine whether the current drag source is allowed as a drop target, when the source is moved on top of the target. It is a script that is executed always when the dragover event is fired for the first time for this source, and returning false will prevent showing any drop effect. The script gets the dragover event as a parameter named event.

The drop criteria is similar to drag over criteria, but it is executed when the user has dropped the data by releasing the mouse button. The script gets the drop event as a parameter named event. Returning false will prevent the drop and no drop event is fired on the server side.

===