FileDocCategorySizeDatePackage
ReorderableJList.javaAPI DocExample8533Mon Jan 09 11:01:58 GMT 2006None

ReorderableJList

public class ReorderableJList extends JList implements DropTargetListener, DragGestureListener, DragSourceListener

Fields Summary
static DataFlavor
localObjectFlavor
static DataFlavor[]
supportedFlavors
DragSource
dragSource
DropTarget
dropTarget
Object
dropTargetCell
int
draggedIndex
Constructors Summary
public ReorderableJList()


       
        super();
        setCellRenderer (new ReorderableListCellRenderer());
        setModel (new DefaultListModel());
        dragSource = new DragSource();
        DragGestureRecognizer dgr = 
            dragSource.createDefaultDragGestureRecognizer (this, 
                                                           DnDConstants.ACTION_MOVE,
                                                           this);
        dropTarget = new DropTarget (this, this);
    
Methods Summary
public voiddragDropEnd(java.awt.dnd.DragSourceDropEvent dsde)

        System.out.println ("dragDropEnd()");
        dropTargetCell = null;
        draggedIndex = -1;
        repaint();
    
public voiddragEnter(java.awt.dnd.DragSourceDragEvent dsde)

public voiddragEnter(java.awt.dnd.DropTargetDragEvent dtde)

        System.out.println ("dragEnter");
        if (dtde.getSource() != dropTarget)
            dtde.rejectDrag();
        else {
            dtde.acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE);
            System.out.println ("accepted dragEnter");
        }

    
public voiddragExit(java.awt.dnd.DragSourceEvent dse)

public voiddragExit(java.awt.dnd.DropTargetEvent dte)

public voiddragGestureRecognized(java.awt.dnd.DragGestureEvent dge)

        System.out.println ("dragGestureRecognized");
        // find object at this x,y
        Point clickPoint = dge.getDragOrigin();
        int index = locationToIndex(clickPoint);
        if (index == -1)
            return;
        Object target = getModel().getElementAt(index);
        Transferable trans = new RJLTransferable (target);
        draggedIndex = index;
        dragSource.startDrag (dge,Cursor.getDefaultCursor(),
                              trans, this);
    
public voiddragOver(java.awt.dnd.DropTargetDragEvent dtde)

        // figure out which cell it's over, no drag to self
        if (dtde.getSource() != dropTarget)
            dtde.rejectDrag();
        Point dragPoint = dtde.getLocation();
        int index = locationToIndex (dragPoint);
        if (index == -1)
            dropTargetCell = null;
        else
            dropTargetCell = getModel().getElementAt(index);
        repaint();
    
public voiddragOver(java.awt.dnd.DragSourceDragEvent dsde)

public voiddrop(java.awt.dnd.DropTargetDropEvent dtde)

        System.out.println ("drop()!");
        if (dtde.getSource() != dropTarget) {
            System.out.println ("rejecting for bad source (" +
                                dtde.getSource().getClass().getName() + ")");
            dtde.rejectDrop();
            return;
        }
        Point dropPoint = dtde.getLocation();
        int index = locationToIndex (dropPoint);
        System.out.println ("drop index is " + index);
        boolean dropped = false;
        try {
            if ((index == -1) || (index == draggedIndex)) {
                System.out.println ("dropped onto self");
                dtde.rejectDrop();
                return;
            }
            dtde.acceptDrop (DnDConstants.ACTION_MOVE);
            System.out.println ("accepted");
            Object dragged =
                dtde.getTransferable().getTransferData(localObjectFlavor);
            // move items - note that indicies for insert will
            // change if [removed] source was before target
            System.out.println ("drop " + draggedIndex + " to " + index);
            boolean sourceBeforeTarget = (draggedIndex < index);
            System.out.println ("source is" +
                                (sourceBeforeTarget ? "" : " not") +
                                " before target");
            System.out.println ("insert at " + 
                                (sourceBeforeTarget ? index-1 : index));
            DefaultListModel mod = (DefaultListModel) getModel();
            mod.remove (draggedIndex);
            mod.add ((sourceBeforeTarget ? index-1 : index), dragged);
            dropped = true;
        } catch (Exception e) {
            e.printStackTrace();
        } 
        dtde.dropComplete (dropped);
    
public voiddropActionChanged(java.awt.dnd.DropTargetDragEvent dtde)

public voiddropActionChanged(java.awt.dnd.DragSourceDragEvent dsde)

public static voidmain(java.lang.String[] args)

        JList list = new ReorderableJList ();
        DefaultListModel defModel = new DefaultListModel();
        list.setModel (defModel);
        String[] listItems = {
            "Chris", "Joshua", "Daniel", "Michael",
            "Don", "Kimi", "Kelly", "Keagan"
        };
        Iterator it = Arrays.asList(listItems).iterator();
        while (it.hasNext())
            defModel.addElement (it.next());
        // show list
        JScrollPane scroller =
            new JScrollPane (list,
                            ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
                            ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        JFrame frame = new JFrame ("Checkbox JList");
        frame.getContentPane().add (scroller);
        frame.pack();
        frame.setVisible(true);