Flex component move using drag and drop in Flex4


 

Flex component move using drag and drop in Flex4

In this example you can see how we can drag and drop the component from one place to another.

In this example you can see how we can drag and drop the component from one place to another.

Flex component move using drag and drop in Flex4:

In this example you can see how we can drag and drop the component from one place to another. There are the three classes of drag and drop operation:

1. DragManager: It manages the drag and drop operation using doDrag() method.
2. DragSource: It contains the data which is dragged.
3. DragEvent: It represent the drag and drop events.

In this example we will drag and drop a Button control in a Panel from one place to another.

Example:

<?xml version="1.0" encoding="utf-8"?>

<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"

xmlns:s="library://ns.adobe.com/flex/spark"

xmlns:mx="library://ns.adobe.com/flex/mx"

minWidth="955" minHeight="600">

<fx:Script>

<![CDATA[

import mx.managers.DragManager;

import mx.core.DragSource;

import mx.events.DragEvent;

import flash.events.MouseEvent;

// initialize the drag and drop operation

private function mouseMoveHandler(event:MouseEvent):void

{

var dragInitiator:Button=Button(event.currentTarget);

var dragSource:DragSource = new DragSource();

dragSource.addData(dragInitiator, "button");

DragManager.doDrag(dragInitiator, dragSource, event);

}

private function dragEnterHandler(event:DragEvent):void {

if (event.dragSource.hasFormat("button"))

{

DragManager.acceptDragDrop(Panel(event.currentTarget));

}

}

private function dragDropHandler(event:DragEvent):void {

Button(event.dragInitiator).x =

Panel(event.currentTarget).mouseX;

Button(event.dragInitiator).y =

0

Panel(event.currentTarget).mouseY;

}

]]>

1

</fx:Script>

<s:Panel title="Flex component move using drag and drop"

width="386" height="282"

2

chromeColor="#555555"

color="#CCCCCC"

dragEnter="dragEnterHandler(event);"

3

dragDrop="dragDropHandler(event);">

<s:Button id="btn" label="Drag Button"

mouseMove="mouseMoveHandler(event)"/>

4

</s:Panel>

</s:Application>

Output:

5

Running Application:

To view this page ensure that Adobe Flash Player version 10.0.0 or greater is installed.

Download this code

6

Ads