Create DragSource object and add data to it : Drag Drop « Development « Flex






Create DragSource object and add data to it

Create DragSource object and add data to it
          
<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
  <mx:Script>
    
      import mx.core.DragSource;
      import mx.containers.Canvas;
      import mx.events.DragEvent;
      import mx.managers.DragManager;

      private function beginDragAndDrop(event:MouseEvent):void {
        var canvas:Canvas = Canvas(event.currentTarget);
        var dragSource:DragSource = new DragSource(  );
        var color:uint = canvas.getStyle("backgroundColor");
        dragSource.addData(color, "backgroundColor");
        var proxy:Canvas = new Canvas(  );
        proxy.width = 50;
        proxy.height = 50;
        proxy.setStyle("backgroundColor", color);
        DragManager.doDrag(canvas, dragSource, event, proxy);
      }

      private function dragEnterHandler(event:DragEvent):void {
        var target:Canvas = Canvas(event.currentTarget);
        var initiator:Canvas = Canvas(event.dragInitiator);
        if(target.getStyle("backgroundColor") ==  initiator.getStyle("backgroundColor")) {
          DragManager.acceptDragDrop(target);
        }
      }

      private function dragDropHandler(event:DragEvent):void {
        var target:Canvas = Canvas(event.currentTarget);
        var initiator:Canvas = Canvas(event.dragInitiator);
        if(target.getStyle("backgroundColor") ==  initiator.getStyle("backgroundColor")) {
          vbox.removeChild(initiator);
        }
      }
  
  </mx:Script>
  <mx:HBox width="100%">
    <mx:VBox id="vbox" height="100%">
      <mx:Canvas width="50" height="50" backgroundColor="#00ff80" mouseDown="beginDragAndDrop(event)" />
    </mx:VBox>
    <mx:VRule height="213"/>
    <mx:HBox id="hbox" height="100%">
       <mx:Canvas width="50" height="50" backgroundColor="#00ff80" dragEnter="dragEnterHandler(event)" dragDrop="dragDropHandler(event)" />
       <mx:Canvas width="50" height="50" backgroundColor="#ff8040" dragEnter="dragEnterHandler(event)" dragDrop="dragDropHandler(event)" />
    </mx:HBox>
  </mx:HBox>


</mx:Application>

   
    
    
    
    
    
    
    
    
    
  








Related examples in the same category

1.Setting the dragMoveEnabled property to move instead of copySetting the dragMoveEnabled property to move instead of copy
2.Dragging and dropping within a component, to allow orderingDragging and dropping within a component, to allow ordering
3.Performing a two-way drag and dropPerforming a two-way drag and drop
4.Dragging and dropping in the same controlDragging and dropping in the same control
5.The drag initiators, and drop targetThe drag initiators, and drop target
6.Get Drag source data formatGet Drag source data format
7.Press control key as drag and dropPress control key as drag and drop
8.Canvas Drag and DropCanvas Drag and Drop
9.Specifying the drag indicator by using the DragSpecifying the drag indicator by using the Drag
10.Specify a Drag ProxySpecify a Drag Proxy
11.Enable and Disable Drag OperationsEnable and Disable Drag Operations
12.Drag Drop To ComponentDrag Drop To Component
13.Set liveDragging to true to dispatch change event continuously as moving the thumbSet liveDragging to true to dispatch change event continuously as moving the thumb
14.Get drag source from DragEventGet drag source from DragEvent
15.Canvas drag enter eventCanvas drag enter event
16.Explpicitly handle the dragOver eventExplpicitly handle the dragOver event