FileDocCategorySizeDatePackage
DNDList.javaAPI DocExample4957Tue Dec 12 18:59:34 GMT 2000None

DNDList

public class DNDList extends JList implements DNDComponentInterface, DropTargetListener, DragGestureListener, DragSourceListener
This is an example of a component, which serves as a DragSource as well as Drop Target. To illustrate the concept, JList has been used as a droppable target and a draggable source. Any component can be used instead of a JList. The code also contains debugging messages which can be used for diagnostics and understanding the flow of events.
version
1.0

Fields Summary
DropTarget
dropTarget
enables this component to be a dropTarget
DragSource
dragSource
enables this component to be a Drag Source
Constructors Summary
public DNDList()
constructor - initializes the DropTarget and DragSource.



            

    
    
    dropTarget = new DropTarget (this, this);
    dragSource = new DragSource();
    dragSource.createDefaultDragGestureRecognizer( this, DnDConstants.ACTION_MOVE, this);
  
Methods Summary
public voidaddElement(java.lang.Object s)
adds elements to itself

        (( DefaultListModel )getModel()).addElement (s.toString());
  
public voiddragDropEnd(java.awt.dnd.DragSourceDropEvent event)
this message goes to DragSourceListener, informing it that the dragging has ended

   
    if ( event.getDropSuccess()){
        removeElement();
    }
  
public voiddragEnter(java.awt.dnd.DropTargetDragEvent event)
is invoked when you are dragging over the DropSite

    
    // debug messages for diagnostics 
    System.out.println( "dragEnter");
    event.acceptDrag (DnDConstants.ACTION_MOVE);
  
public voiddragEnter(java.awt.dnd.DragSourceDragEvent event)
this message goes to DragSourceListener, informing it that the dragging has entered the DropSite

    System.out.println( " dragEnter");
  
public voiddragExit(java.awt.dnd.DragSourceEvent event)
this message goes to DragSourceListener, informing it that the dragging has exited the DropSite

    System.out.println( "dragExit");
    
  
public voiddragExit(java.awt.dnd.DropTargetEvent event)
is invoked when you are exit the DropSite without dropping

    System.out.println( "dragExit");
    
  
public voiddragGestureRecognized(java.awt.dnd.DragGestureEvent event)
a drag gesture has been initiated

    
    Object selected = getSelectedValue();
    if ( selected != null ){
        StringSelection text = new StringSelection( selected.toString()); 
        
        // as the name suggests, starts the dragging
        dragSource.startDrag (event, DragSource.DefaultMoveDrop, text, this);
    } else {
        System.out.println( "nothing was selected");   
    }
  
public voiddragOver(java.awt.dnd.DragSourceDragEvent event)
this message goes to DragSourceListener, informing it that the dragging is currently ocurring over the DropSite

    System.out.println( "dragExit");
    
  
public voiddragOver(java.awt.dnd.DropTargetDragEvent event)
is invoked when a drag operation is going on

    System.out.println( "dragOver");
  
public voiddrop(java.awt.dnd.DropTargetDropEvent event)
a drop has occurred

    
    try {
        Transferable transferable = event.getTransferable();
                   
        // we accept only Strings      
        if (transferable.isDataFlavorSupported (DataFlavor.stringFlavor)){
        
            event.acceptDrop(DnDConstants.ACTION_MOVE);
            String s = (String)transferable.getTransferData ( DataFlavor.stringFlavor);
            addElement( s );
            event.getDropTargetContext().dropComplete(true);
        } 
        else{
            event.rejectDrop();
        }
    }
    catch (IOException exception) {
        exception.printStackTrace();
        System.err.println( "Exception" + exception.getMessage());
        event.rejectDrop();
    } 
    catch (UnsupportedFlavorException ufException ) {
      ufException.printStackTrace();
      System.err.println( "Exception" + ufException.getMessage());
      event.rejectDrop();
    }
  
public voiddropActionChanged(java.awt.dnd.DragSourceDragEvent event)
is invoked when the user changes the dropAction

    System.out.println( "dropActionChanged"); 
  
public voiddropActionChanged(java.awt.dnd.DropTargetDragEvent event)
is invoked if the use modifies the current drop gesture

  
public voidremoveElement()
removes an element from itself

    (( DefaultListModel)getModel()).removeElement( getSelectedValue());