Today I am going to run through a smallish tutorial on creating and using drag and drop in Flex using the DragManager class, which is a utility provided to make drag and drop easy. I should also mention, right off the bat, that some things like lists already have various drag and drop features built in. I however will be showing an example that is useful for any custom drag jobs you might need to do.
The application below shows the functionality that I will be building today. It is a very simple demo that has a few games up at the top that can be added to your shopping cart by dragging them down into the basket area. Once dropped into the "basket" they will show up in the shopping cart list. The interface is simple and the code is simple - and you can grab that code using this link: Drag and Drop Source Code.
<?xml version="1.0" encoding="utf-8"?>
<mx:Application
xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute" width="374" height="406">
<mx:Label x="0" y="210" text="Games"
width="118" height="42" fontSize="28" color="#818181"/>
<mx:Image name="Bioshock" x="2" y="2"
source="{bioshock_icon}" mouseDown="doDrag(event)" />
<mx:Image name="Crysis" x="120" y="2"
source="{crysis_icon}" mouseDown="doDrag(event)" />
<mx:Image name="Halo" x="240" y="2"
source="{halo_icon}" mouseDown="doDrag(event)" />
<mx:Image name="Neverwinter Nights" x="50" y="110"
source="{neverwinter_icon}" mouseDown="doDrag(event)" />
<mx:Image name="World of Warcraft" x="200" y="110"
source="{wow_icon}" mouseDown="doDrag(event)" />
<mx:Canvas x="0" y="255" width="174" height="151"
borderStyle="solid" backgroundColor="#12976A"
dragEnter="dragAccept(event)"
dragDrop="dragDrop(event)">
<mx:Label x="0" y="0" text="Basket"
width="118" height="42" fontSize="28" color="#FFFFFF"/>
</mx:Canvas>
<mx:DataGrid dataProvider="{cartContents}"
x="173" y="255" width="201" height="151">
<mx:columns>
<mx:DataGridColumn headerText="Name" dataField="name"/>
<mx:DataGridColumn headerText="Quantity" dataField="num"
width="65"/>
</mx:columns>
</mx:DataGrid>
<mx:Label x="272" y="235" text="Shopping Cart" color="#3A3A3A"
fontWeight="bold" fontSize="12"/>
</mx:Application>