Drag and drop can be an important feature in web apps. It allows adding a human touch to UIs. Drag and drag can be easily implemented using Html5 and Javascript, and also using other frameworks such as jquery. For Java web development, GWT provides a good solution. GWT have added a native feature to enable drag and drop for widgets and components. We are going to walk through it in this tutorial.
As the name implies, drag and drop has two sides: the element to be dragged, and the element to be dropped on.
To enable drag for any widget or component in GWT, we have to use the addDragHandler method. For example:
Not all elements have the addDragHandler by default. If an element is not implementing the DragHandler interface, the developper needs to add the handler to the DOM handlers:
For the drop, we need to implement two handlers: DropHandler and DragOverHandler. Suppose we want to use a FlowPanel as a drop target:
That’s it. Now we can drag the Image into the FlowPanel.
There are other GWT libraries that implements Drag and Drop out of the box such as: