DND Drag and drop List : List « Swing Components « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Swing Components » List 




DND Drag and drop List
 


/**
 * 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
 */

import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DragGestureEvent;
import java.awt.dnd.DragGestureListener;
import java.awt.dnd.DragSource;
import java.awt.dnd.DragSourceDragEvent;
import java.awt.dnd.DragSourceDropEvent;
import java.awt.dnd.DragSourceEvent;
import java.awt.dnd.DragSourceListener;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetEvent;
import java.awt.dnd.DropTargetListener;

import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.ListModel;


public class DNDList extends JList implements DropTargetListener, DragSourceListener, DragGestureListener
{

  /**
   * enables this component to be a dropTarget
   */

  DropTarget dropTarget = null;

  /**
   * enables this component to be a Drag Source
   */
  DragSource dragSource = null;

  /**
   * constructor - initializes the DropTarget and DragSource.
   */

  public DNDListListModel dataModel )
  {
    superdataModel );
    dropTarget = new DropTargetthis, this );
    dragSource = new DragSource();
    dragSource.createDefaultDragGestureRecognizerthis, DnDConstants.ACTION_MOVE, this );
  }

  /**
   * is invoked when you are dragging over the DropSite
   
   */

  public void dragEnterDropTargetDragEvent event )
  {

    // debug messages for diagnostics
    System.out.println"dragEnter" );
    event.acceptDragDnDConstants.ACTION_MOVE );
  }

  /**
   * is invoked when you are exit the DropSite without dropping
   
   */

  public void dragExitDropTargetEvent event )
  {
    System.out.println"dragExit" );

  }

  /**
   * is invoked when a drag operation is going on
   
   */

  public void dragOverDropTargetDragEvent event )
  {
    System.out.println"dragOver" );
  }

  /**
   * a drop has occurred
   
   */

  public void dropDropTargetDropEvent event )
  {

    try
    {
      Transferable transferable = event.getTransferable();

      // we accept only Strings
      iftransferable.isDataFlavorSupportedDataFlavor.stringFlavor ) )
      {

        event.acceptDropDnDConstants.ACTION_MOVE );
        String s = String )transferable.getTransferDataDataFlavor.stringFlavor );
        addElement);
        event.getDropTargetContext().dropCompletetrue );
      }
      else
      {
        event.rejectDrop();
      }
    }
    catchException exception )
    {
      System.err.println"Exception" + exception.getMessage() );
      event.rejectDrop();
    }
  }

  /**
   * is invoked if the use modifies the current drop gesture
   
   */

  public void dropActionChangedDropTargetDragEvent event )
  {
  }

  /**
   * a drag gesture has been initiated
   
   */

  public void dragGestureRecognizedDragGestureEvent event )
  {

    Object selected = getSelectedValue();
    ifselected != null )
    {
      StringSelection text = new StringSelectionselected.toString() );

      // as the name suggests, starts the dragging
      dragSource.startDragevent, DragSource.DefaultMoveDrop, text, this );
    }
    else
    {
      System.out.println"nothing was selected" );
    }
  }

  /**
   * this message goes to DragSourceListener, informing it that the dragging
   * has ended
   
   */

  public void dragDropEndDragSourceDropEvent event )
  {
    ifevent.getDropSuccess() )
    {
      removeElement();
    }
  }

  /**
   * this message goes to DragSourceListener, informing it that the dragging
   * has entered the DropSite
   
   */

  public void dragEnterDragSourceDragEvent event )
  {
    System.out.println" dragEnter" );
  }

  /**
   * this message goes to DragSourceListener, informing it that the dragging
   * has exited the DropSite
   
   */

  public void dragExitDragSourceEvent event )
  {
    System.out.println"dragExit" );

  }

  /**
   * this message goes to DragSourceListener, informing it that the dragging is
   * currently ocurring over the DropSite
   
   */

  public void dragOverDragSourceDragEvent event )
  {
    System.out.println"dragExit" );

  }

  /**
   * is invoked when the user changes the dropAction
   
   */

  public void dropActionChangedDragSourceDragEvent event )
  {
    System.out.println"dropActionChanged" );
  }

  /**
   * adds elements to itself
   
   */

  public void addElementObject s )
  {
    ( ( DefaultListModel )getModel() ).addElements.toString() );
  }

  /**
   * removes an element from itself
   */

  public void removeElement()
  {
    ( ( DefaultListModel )getModel() ).removeElementgetSelectedValue() );
  }

}

   
  














Related examples in the same category
1.CheckBox List by Zhiguo YinCheckBox List by Zhiguo Yin
2.Mouse Roll over ListMouse Roll over List
3.Check List ExampleCheck List Example
4.Check List Example 2Check List Example 2
5.ToolTip List ExampleToolTip List Example
6.Editable List ExampleEditable List Example
7.CheckBox List
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.