Tuesday, May 31, 2016

Drag and Drop between JLists (1)

Sample code of using the DragSourceListener, DragGestureListener, and DropTargetListener to drag element from one list to another.

import java.awt.BorderLayout;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
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 java.io.IOException;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JSplitPane;

public class DragDropDemo extends JFrame {
    private SourceList<String> source;
    private TargetList<String> target;
 
    public DragDropDemo() {
        source = new SourceList<String>(new String[]{"Papaya", "Orange", "Apple", "Mango", "Pear", "Avakado"});
        target = new TargetList<String>();
     
        JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, source, target);
        JLabel label = new JLabel("Please drag your selection from the left list to the right");
     
        add(splitPane);
        add(label, BorderLayout.NORTH);
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        pack();
        setVisible(true);
    }
 
    private class SourceList<E> extends JList implements DragSourceListener, DragGestureListener {
        DragSource dragSource;
     
        public SourceList(E[] data){
            super(data);
            dragSource = new DragSource();
            dragSource.createDefaultDragGestureRecognizer(this, DnDConstants.ACTION_COPY_OR_MOVE, this);
        }
     
        public void dragDropEnd(DragSourceDropEvent e){
            System.out.println("Finished dragging !");
        }
        public void dragExit(DragSourceEvent e){
            System.out.println("Exiting dragging !");
        }
        public void dropActionChanged(DragSourceDragEvent e){
            System.out.println("Changing drag action !");
        }
        public void dragOver(DragSourceDragEvent e){
            System.out.println("Over dragging !");
        }
        public void dragEnter(DragSourceDragEvent e){
            System.out.println("Entering dragging !");
        }
     
        public void dragGestureRecognized(DragGestureEvent e){
            Transferable transferable = new StringSelection((String)getSelectedValue());
            dragSource.startDrag(e, DragSource.DefaultCopyDrop, transferable, this);
        }
    }
 
    private class TargetList<E> extends JList implements DropTargetListener {
        DefaultListModel model = new DefaultListModel();
        public TargetList() {
            super();
            setModel(model);
            new DropTarget(this, this);
        }
     
        public void drop (DropTargetDropEvent e){
            Transferable transferable = e.getTransferable();
            if (!transferable.isDataFlavorSupported(DataFlavor.stringFlavor)){
                e.rejectDrop();
            }
            e.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
            String dragged;
            try {
                dragged = (String)transferable.getTransferData(DataFlavor.stringFlavor);
                model.addElement(dragged);
                validate();
            }catch(UnsupportedFlavorException fe){
                e.rejectDrop();
            }catch(IOException ie){
                e.rejectDrop();
            }
        }

        public void dragExit(DropTargetEvent e) {
            System.out.println("Exiting Dropping !");
        }
     
        public void dropActionChanged(DropTargetDragEvent e){
            System.out.println("Changing Drop Action !");
        }
        public void dragOver(DropTargetDragEvent e){
            System.out.println("Over Dropping !");
        }
        public void dragEnter(DropTargetDragEvent e){
            System.out.println("Entering Dropping !");
        }
    }
 
    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new DragDropDemo();
            }
        });
    }
}

Further Readings:
1. Drag and Drop between JLists (2)
2. Drag and Drop between JLists (3)

---------------------------------------------------------------------------------------------------------------

                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.

No comments:

Post a Comment