Tuesday, May 31, 2016

Drag and Drop between JLists (3)

Sample code for dragging Icons from one list to another.

import java.awt.BorderLayout;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import javax.swing.DefaultListModel;
import javax.swing.DropMode;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JSplitPane;
import javax.swing.TransferHandler;

public class DragDropDemo2 extends JFrame {
    private JList<ImageIcon> source;
    private JList<ImageIcon> target;
    private DefaultListModel<ImageIcon> model = new DefaultListModel<ImageIcon>();
    private String imageLoc = "C:/images/";
 
    public DragDropDemo2() {
        source = new JList<ImageIcon>(new ImageIcon[]{new ImageIcon(imageLoc+"open.gif"),
                        new ImageIcon(imageLoc+"paste.gif"), new ImageIcon(imageLoc+"print.gif")});
        target = new JList<ImageIcon>();
        target.setModel(model);
     
        source.setDragEnabled(true);
        target.setDropMode(DropMode.ON_OR_INSERT);
     
        source.setTransferHandler(new ExportTransferHandler());
        target.setTransferHandler(new ImportTransferHandler());
     
        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 ExportTransferHandler extends TransferHandler {
        public int getSourceActions(JComponent c){
            return TransferHandler.COPY_OR_MOVE;
        }
     
        public Transferable createTransferable(JComponent c) {
            return new MyTransferable();
        }
    }
 
    private class MyTransferable implements Transferable {
        public Object getTransferData(DataFlavor flavor){
            if (flavor == DataFlavor.imageFlavor){
                return source.getSelectedValue();
            }
            return null;
        }
     
        public DataFlavor[] getTransferDataFlavors(){
            return new DataFlavor[] {DataFlavor.imageFlavor};
        }
     
        public boolean isDataFlavorSupported(DataFlavor flavor){
            if (flavor.equals(DataFlavor.imageFlavor)){
                return true;
            }
            return false;
        }
    }
 
    private class ImportTransferHandler extends TransferHandler {

        public boolean canImport(TransferHandler.TransferSupport supp) {
            if (!supp.isDataFlavorSupported(DataFlavor.imageFlavor)) {
                return false;
            }
            return true;
        }
     
        public boolean importData(TransferHandler.TransferSupport supp) {
            // Fetch the Transferable and its data
            Transferable t = supp.getTransferable();
            ImageIcon selected = null;
            try {
                selected = (ImageIcon)t.getTransferData(DataFlavor.imageFlavor);
            } catch (Exception e){
                System.out.println(e.getMessage());
                return false;
            }

            // Fetch the drop location
            JList.DropLocation loc = target.getDropLocation();
            int row = loc.getIndex();
            model.add(row, selected);
            target.validate();
            return true;
        }
    }
 
    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new DragDropDemo2();
            }
        });
    }
}

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

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

                        
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