Drag and Drop Component within Vaadin 8 AbsoluteLayout

The following code currently has a snap effect if the startDrag mouse location is centered in the component. When the component is dropped, the upper left portion of the component snaps to the drop location. To fix it, I need the relative location of the mouse when the drag started, so I can calculate the deltaX and deltaY, rather than just moving the component to the drop cursor location.

But, I cannot figure out how to get the cursor location when the drag starts. I am using Vaadin 8.4.2.

The code snippet below shows what I am currently doing. But I cannot figure out how to calculate deltaX and deltaY in the commented out section.

  DropTargetExtension<AbsoluteLayout> dropTarget = new DropTargetExtension<>(absLayout);
  dropTarget.setDropEffect(DropEffect.COPY);

  dropTarget.addDropListener(event-> {
	 MouseEventDetails details = event.getMouseEventDetails();

	 // Get the location of the component before it gets moved
	 ComponentPosition pos = commander.getPosition(event.getDragSourceComponent().get());

	 // TODO Calculate the drag coordinate difference
	 //int deltaX = currentMousePositionX - pos.getLeftValue();
	 //int deltaY = currentMousePOsitionY - pos.getTopValue();
		
	 int x = details.getRelativeX();
	 int y = details.getRelativeY();

	 pos.setLeftValue((float) x); // TODO pos.setLeftValue((float) pos.getLeftValue() + deltaX);
	 pos.setTopValue((float) y); // TODO pos.setTopValue((float) pos.getTopValue() + deltaY);
  });

I think the correct way would be to save the position in DragStartEvent with dragSource.setDataTransferData(…) method and get the data in DropEvent using event.getDragData(…)

Tatu Lund:
I think the correct way would be to save the position in DragStartEvent with dragSource.setDataTransferData(…) method and get the data in DropEvent using event.getDragData(…)

I like that idea, but how do I get the mouse location at the start of the drag? It isn’t enough to have the location of the component, but rather, I need to know the location of the mouse so I can calculate the delta movement.

Eric Wetmiller:
The following code currently has a snap effect if the startDrag mouse location is centered in the component. When the component is dropped, the upper left portion of the component snaps to the drop location. To fix it, I need the relative location of the mouse when the drag started, so I can calculate the deltaX and deltaY, rather than just moving the component to the drop cursor location.

But, I cannot figure out how to get the cursor location when the drag starts. I am using Vaadin 8.4.2.

The code snippet below shows what I am currently doing. But I cannot figure out how to calculate deltaX and deltaY in the commented out section.

  DropTargetExtension<AbsoluteLayout> dropTarget = new DropTargetExtension<>(absLayout);
  dropTarget.setDropEffect(DropEffect.COPY);

  dropTarget.addDropListener(event-> {
	 MouseEventDetails details = event.getMouseEventDetails();

	 // Get the location of the component before it gets moved
	 ComponentPosition pos = commander.getPosition(event.getDragSourceComponent().get());

	 // TODO Calculate the drag coordinate difference
	 //int deltaX = currentMousePositionX - pos.getLeftValue();
	 //int deltaY = currentMousePOsitionY - pos.getTopValue();
		
	 int x = details.getRelativeX();
	 int y = details.getRelativeY();

	 pos.setLeftValue((float) x); // TODO pos.setLeftValue((float) pos.getLeftValue() + deltaX);
	 pos.setTopValue((float) y); // TODO pos.setTopValue((float) pos.getTopValue() + deltaY);
  });

You can simply check it on [Avast Support]
(http://chathelp.org/avast-support/) I think this site help you a lot I also take a help of this site to solve out the issue.