I followed the instructions given here: Drag and Drop in GWT 2.4
It works well, but I now want to select between many objects which to transfer. How can it be determined which object is dragged?
Thank you in advance!
I followed the instructions given here: Drag and Drop in GWT 2.4
It works well, but I now want to select between many objects which to transfer. How can it be determined which object is dragged?
Thank you in advance!
I would do the following:
1) Create a map with objects allowed for dragging (Labels in your example)
HashMap <String, Label> draggableWidgetsMap = new HashMap<String, Label>();
draggableWidgetsMap.add("key1", widget1);
...
draggableWidgetsMap.add("keyN", widgetN);
2) Then apply the dragStart event handler for each widget, e.g. for widget1
it would be:
widget1.addDomHandler(new DragStartHandler() {
@Override
public void onDragStart(DragStartEvent event) {
event.setData("widgetKey", "key1");
}
}, DragStartEvent.getType());
3) And then you can check what widget is dragged now in DragOver event handler:
widget1.addDomHandler(new DragOverHandler() {
@Override
public void onDragOver(DragOverEvent event) {
String widgetKey = event.getDataTransfer().getData("widgetKey");
if (widgetKey != null && widgetKey.length() > 0){
if (draggableWidgetsMap.containsKey(widgetKey)){
// Print out Label's text to see which one is dragged
System.out.println("Dragged label has text: \" " + draggableWidgetsMap.get(widgetKey).getText() + "\"")
// do something relevant with this Label
}
}
}
}, DragOverEvent.getType());
Certainly, I would loop through all the widgets to apply both handlers for the widgets you are going to make draggable.