title: java drag and drop
body: <p>I try to get familar with drag and drop in java but all the tutorials I found were... (getting me angry)</p>
  
  <p>All I want is to drag a "PublicUserLabel" from a JList (included in a selfmade JPanel called "UserPanel") and drop it in a selfmade class inerited from JTabbedPanel.
  It is very important to drag the object itself and not its stringrepresentation!!!</p>
  
  <p>That is what I have so far:
  PublicUserLabel</p>
  
  <pre><code>public class PublicUserLabel extends JLabel implements DragSourceListener, DragGestureListener, Transferable
      {
          private DragSource ds;
          private PublicUser user;
  
          public PublicUserLabel(PublicUser user)
          {
              super(user.getName());
              this.user = user;
              ds = new DragSource();
              ds.createDefaultDragGestureRecognizer(this, DnDConstants.ACTION_COPY_OR_MOVE, this);
          }
  
          @Override
          public void dragGestureRecognized(DragGestureEvent e)
          {
              ds.startDrag(e, DragSource.DefaultCopyDrop, this, this);
          }
  
          @Override
          public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException
          {
              if (flavor.equals(PublicUserFlavor.publicUserFlavor))
              {
                  return this;//TODO ?
              }
              throw new UnsupportedFlavorException(flavor);
          }
  
          @Override
          public DataFlavor[] getTransferDataFlavors()
          {
              DataFlavor[] df = new DataFlavor[2];
              df[0] = DataFlavor.stringFlavor;
              df[1] = PublicUserFlavor.publicUserFlavor;
              return df;
          }
  
          @Override
          public boolean isDataFlavorSupported(DataFlavor flavor)
          {
              return flavor.equals(PublicUserFlavor.publicUserFlavor);
          }
  
      //some more methods
      }
  </code></pre>
  
  <p>UserPanel:</p>
  
  <pre><code>public class UserPanel extends JPanel
  {
      private JTextField search;
      private List&lt;PublicUser&gt; allUser;
      private JList&lt;PublicUserLabel&gt; list;
      private JScrollPane scrollPane;
      private DefaultListModel&lt;PublicUserLabel&gt; listModel;
  
      public UserPanel()
      {
          allUser = new LinkedList&lt;PublicUser&gt;();
          listModel = new DefaultListModel&lt;PublicUserLabel&gt;();
          list = new JList&lt;PublicUserLabel&gt;(listModel);
          list.setDragEnabled(true);
  </code></pre>
  
  <p>PublicUserFlavor:</p>
  
  <pre><code>public class PublicUserFlavor extends DataFlavor
  {
      public static DataFlavor publicUserFlavor;
  
      static
      {
          publicUserFlavor = new DataFlavor(PublicUser.class, "PublicUser");
      }
  }
  </code></pre>
  
  <p>TabPanel:</p>
  
  <pre><code>public class TabPanel extends JTabbedPane implements DropTargetListener
  {
      public TabPanel()
      {
          setTabPlacement(JTabbedPane.BOTTOM);
          addNewTabComponent("bla");
          addNewTabComponent("blub");
          setDropTarget(new DropTarget(this, this));
      }
  
      @Override
      public void drop(DropTargetDropEvent e)
      {
          Transferable transferable = e.getTransferable();
          if (transferable.isDataFlavorSupported(PublicUserFlavor.publicUserFlavor))
          {
              e.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
              try
              {
                  Object o = transferable.getTransferData(PublicUserFlavor.publicUserFlavor);
                  System.out.println(o);
                  if (o instanceof PublicUserLabel)
                  {
                      PublicUserLabel l = (PublicUserLabel)o;
                      PublicUser u = l.getUser();
                      System.out.println(u);
                  }
              }
              catch (UnsupportedFlavorException e1)
              {
                  // TODO Auto-generated catch block
                  e1.printStackTrace();
              }
              catch (IOException e1)
              {
                  // TODO Auto-generated catch block
                  e1.printStackTrace();
              }
              e.getDropTargetContext().dropComplete(true);
          }
      }
  </code></pre>
  
  <p>In the drop method are some syso's that shall be executed if a User is droped in the panel. But that is not the fact. Am I doing something completly wrong?
  Thank you for helping!</p>