Share on Google+Share on Google+

Flex component move using drag and drop in Flex4

Ads
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 =

Panel(event.currentTarget).mouseY;

}

]]>

</fx:Script>

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

width="386" height="282"

chromeColor="#555555"

color="#CCCCCC"

dragEnter="dragEnterHandler(event);"

dragDrop="dragDropHandler(event);">

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

mouseMove="mouseMoveHandler(event)"/>

</s:Panel>

</s:Application>

Output:

Running Application:

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

Download this code

Advertisements

Ads

Posted on: July 12, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Advertisement