- Vaadin 7 Cookbook
- Jaroslav Holaň Ond?ej Kvasnovsk?
- 451字
- 2021-07-23 14:19:35
Dragging-and-dropping between different layouts
Each layout has a different wrap behavior. If we want to try this behavior for ourselves, we will have to create a simple demo. In this demo, we can drag-and-drop components between the four different layouts. We can also change the size of each layout by moving the separator and watch how components are wrapped. If the line cannot be wrapped, a scroll bar appears.
How to do it...
Carry out the following steps to create a drag and drop panel:
- We create project with the root
Demo
class.public class Demo extends UI {…}
- All four layouts will be inserted into the
DragndropPanel
class that extendsHorizontalSplitPanel
.public class DragndropPanel extends HorizontalSplitPanel {…}
- Now let's insert the
createLayout()
method. In this method we add buttons to the layout that is taken over theAbstractLayout
parameter. Next we wrap this layout by theDragAndDropWrapper
class and setDropHandler
. Here we implement two methods. First isgetAcceptCriterion()
that accepts all components and second is thedrop()
method in which we add the dropped component into the layout.private Component createLayout(final AbstractLayout layout) { layout.addComponent(createButton("one")); layout.addComponent(createButton("two")); layout.addComponent(createButton("three")); layout.addComponent(createButton("four")); DragAndDropWrapper dndLayout = new DragAndDropWrapper(layout); dndLayout.setSizeFull(); dndLayout.setDropHandler(new DropHandler() { @Override public AcceptCriterion getAcceptCriterion() { return AcceptAll.get(); } @Override public void drop(DragAndDropEvent event) { WrapperTransferable t = (WrapperTransferable) event.getTransferable(); layout.addComponent(t.getSourceComponent()); } }); return dndLayout; }
- Buttons are created in a different method because we also need to wrap them by
DragAndDropWrapper
.private Component createButton(String name) { Button button = new Button(name); DragAndDropWrapper buttonWrap = new DragAndDropWrapper(button); buttonWrap.setDragStartMode(DragStartMode.COMPONENT); buttonWrap.setSizeUndefined(); return buttonWrap; }
- Now we continue with a constructor. Here we create two
VerticalSplitPanels
. We insert the first on the left side ofHorizontalSplitPanel
and the second on the right side. Now we have four parts ofSplitPanel
, where we insertHorizontalLayout
,VerticalLayout
,GridLayout
, andCssLayout
.public DragndropPanel() { VerticalSplitPanel leftSplitPanel = new VerticalSplitPanel(); leftSplitPanel.setSizeFull(); leftSplitPanel.setFirstComponent( createLayout(new HorizontalLayout())); leftSplitPanel.setSecondComponent( createLayout(new VerticalLayout())); VerticalSplitPanel rightSplitPanel = new VerticalSplitPanel(); rightSplitPanel.setSizeFull(); rightSplitPanel.setFirstComponent( createLayout(new GridLayout(3, 3))); rightSplitPanel.setSecondComponent( createLayout(new CssLayout() { @Override protected String getCss(Component c) { return "display: inline-block;"; } })); setFirstComponent(leftSplitPanel); setSecondComponent(rightSplitPanel); setSizeFull(); }
- Now our
DragndropPanel
constructor is done. We can use it in the main UIDemo
class in theinit()
method.public class Demo extends UI { @Override public void init(VaadinRequest request) { setContent(new DragndropPanel()); } }
How it works...
In Vaadin, we can drag-and-drop components simply by wrapping them inside DragAndDropWrapper
. We can enable dragging by calling the setDragStartMode(DragStartMode mode)
method. For dropping a component, we have to define an accepted criterion on the target component and drop the component by the drop(DragAndDropEvent event)
method.
See also
- More information about drag-and-drop is described on the Vaadin web page at https://vaadin.com/book/vaadin7/-/page/advanced.dragndrop.html