Important Notice - Forums is archived
To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.
drop handler not working for DDLayouts
I can't get the destination layout(DDHorizntalLayout) to accept the custom component from a DDGridLayout. Here is a snippet of the code:
public AcceptCriterion getAcceptCriterion()
{
return AcceptAll.get();
}
public void drop(DragAndDropEvent event)
{
LayoutBoundTransferable t = (LayoutBoundTransferable) event.getTransferable();
Component c = t.getComponent();
......
}
The source layout is a DDGridLayout:
DDGridLayout facesGrid;
facesGrid.setDragMode(LayoutDragMode.CLONE);
PictureInfo currPic = new PictureInfo(new Label(name), cb, img);
facesGrid.addComponent(currPic);
I see it being dragged but it won't stay in the DDHorizontalLayout. I also see it hit the AcceptCriterion.
One thing I can think of (at least I didn't see it in your code) is that you are not detaching the custom component from the grid layout before attaching it to the horizontal layout. At least for me this example works:
DDGridLayout grid = new DDGridLayout();
grid.setDragMode(LayoutDragMode.CLONE);
DDHorizontalLayout horizontal = new DDHorizontalLayout();
horizontal.setDropHandler(new DropHandler() {
public AcceptCriterion getAcceptCriterion() {
return AcceptAll.get();
}
public void drop(DragAndDropEvent event) {
LayoutBoundTransferable transferable = (LayoutBoundTransferable)event.getTransferable();
HorizontalLayoutTargetDetails details = (HorizontalLayoutTargetDetails) event.getTargetDetails();
Component c = transferable.getComponent();
// Detach from parent layout
((ComponentContainer)c.getParent()).removeComponent(c);
// Add to target layout (ignores position)
((DDHorizontalLayout)details.getTarget()).addComponent(c);
}
});
With the addon comes a default implementation of a DDHorizontalLayout drophandler, DefaultHorizontalLayoutDropHandler, which does this and also takes into consideration the drop location and special cases. Take a look at it for an example.
Note: The released version of DefaultHorizontalLayoutDropHandler.java (0.6.3) contains a couple of typos which has been fixed after the release so don't use it directly, instead use the version in the repository. The fixed version will be included in the next release (0.6.4)
My Buttons in GridLayout is not draggable.
Here is my code
public class DragDropGridLayoutDemo {
private static final long serialVersionUID = 1L;
private static final int COLUMNS = 4;
private static final int ROWS = 4;
public DragDropGridLayoutDemo() {
}
public Component getLayout() {
// start-source
DDVerticalLayout outer = new DDVerticalLayout();
outer.setSizeFull();
outer.setDragMode(LayoutDragMode.CLONE);
outer.setDropHandler(new DefaultVerticalLayoutDropHandler());
Label lbl = new Label("This is a grid layout with 16 cells, try dragging the buttons into an empty cell");
outer.addComponent(lbl);
// Create a drag and droppable grid layout
final DDGridLayout layout = new DDGridLayout(COLUMNS, ROWS);
layout.setWidth("400px");
layout.setHeight("400px");
// Only allow dropping in the center of the grid layout cell
layout.setComponentHorizontalDropRatio(0);
layout.setComponentVerticalDropRatio(0);
outer.addComponent(layout);
outer.setExpandRatio(layout, 1);
// Enable dragging components
layout.setDragMode(LayoutDragMode.CLONE);
// Enable dropping components
layout.setDropHandler(new DefaultGridLayoutDropHandler());
for (int row = 0; row < ROWS; row++) {
for (int col = 0; col < COLUMNS; col++) {
if (row == 0 || row == ROWS - 1 || col == 0 || col == COLUMNS - 1) {
Button btn = new Button("Button");
layout.addComponent(btn, col, row);
layout.setComponentAlignment(btn, Alignment.MIDDLE_CENTER);
}
}
}
// end-source
return outer;
}
}
DragDroplayouts contains client side GWT code that needs to be compiled into the widgetset. If you want to use it you need to replace DefaultWidgetset with your own widgetset.
The same goes for most addons in the Directory.