title: Reordering JList with Drag and Drop
body: <p>I encountered a problem regarding reordering elements in a JList using Drag and Drop. This following code is a modification of a code where you could drag elements from one JList to another (worked only one way). I tried to make it usable for only one JList, but the elements can't even be dragged out of the list. So I guess it can't be done this way. Any ideas what I'm doing wrong or not taking into consideration?</p>
  
  <p>The idea is to get it to work for a Jlist with thumbnails, but since I can't even get it to work with just strings... I have been looking into several D'n'D tutorials, but still I can't get it to work. 
  Any help is appreciated. </p>
  
  <pre><code>import javax.swing.*;
  import javax.swing.border.*;
  import java.awt.*;
  import java.awt.datatransfer.*;
  import java.io.IOException;
  
  public class DragAndDrop extends JFrame {
  
  
  DefaultListModel&lt;String&gt; transport = new DefaultListModel&lt;String&gt;();
  JList&lt;String&gt; transportList = new JList&lt;String&gt;(transport);
  
  public DragAndDrop() {
      setLayout(new FlowLayout());
  
      transport.addElement("Bike");
      transport.addElement("Car");
      transport.addElement("Truck");
      transport.addElement("Boat");
  
      JScrollPane transportScroll = new JScrollPane(transportList);
      transportScroll.setBorder(new TitledBorder("Transportation"));
  
      add(transportScroll);
  
      transportList.setDragEnabled(true);
  
      transportList.setTransferHandler(new TransferHandler() {
          int index;  
  
          @Override
          public int getSourceActions(JComponent comp) {
              return COPY_OR_MOVE;
          }
  
          @Override
          public Transferable createTransferable(JComponent comp) {
              index = transportList.getSelectedIndex(); 
              return new StringSelection(transportList.getSelectedValue());
          }
  
  
          @Override
          public void exportDone( JComponent comp, Transferable trans, int action ) {
              if (action==MOVE) {
                  transport.remove(index);
              }
          }
      });
  
      transportList.setDropMode(DropMode.ON);
  
      transportList.setTransferHandler(new TransferHandler() {
          @Override
          public boolean canImport(TransferHandler.TransferSupport support) {
              // data of type string?
              return support.isDataFlavorSupported(DataFlavor.stringFlavor);
          }
  
          @Override
          public boolean importData(TransferHandler.TransferSupport support) {
              try {
                  // convert data to string
                  String s = (String)support.getTransferable().getTransferData(DataFlavor.stringFlavor);
                  JList.DropLocation dl = (JList.DropLocation)support.getDropLocation();
                  transport.add(dl.getIndex(),s);
                  return true;
              } 
              catch (UnsupportedFlavorException e) {} 
              catch (IOException e) {}
  
              return false;
          }
      });
  
      pack();
  
      setDefaultCloseOperation(EXIT_ON_CLOSE);
      setVisible(true);
  }
  
  public static void main(String[] args) {
      new DragAndDrop();
  }
  
  }
  </code></pre>
  
  <p>PS. Sorry if this turns out to be a re-post. </p>
  
  <p><strong>EDIT</strong>
  I think I got it fixed: had to different transferHandlers - should only have one with all the methods from the second one as well.</p>