drag and Drop for a JLabel: how to get the drag source

I am working on a application where it is possible to drop the content of one JLabel into the other one. I use for this the class NameSlot:

public class NameSlot extends JLabel implements DropTargetListener{

NameSlot(String name){
    super(name);
    new DropTarget(this,this);

    this.setTransferHandler(new TransferHandler("foreground"));
    MouseListener listener = new MouseAdapter() {
      public void mousePressed(MouseEvent me) {
        JLabel comp = (JLabel) me.getSource();
        TransferHandler handler = comp.getTransferHandler();
        handler.exportAsDrag(comp, me, TransferHandler.COPY);
      }
    };
    this.addMouseListener(listener);
}



@Override
public void dragEnter(DropTargetDragEvent dtde) {
    // TODO Auto-generated method stub

}


@Override
public void dragExit(DropTargetEvent dte) {
    // TODO Auto-generated method stub

}


@Override
public void dragOver(DropTargetDragEvent dtde) {
    // TODO Auto-generated method stub

}


@Override
public void drop(DropTargetDropEvent dtde) {

//      DropTarget dt = (DropTarget) dtde.getSource();
//      NameSlot ns  = (NameSlot) dt.getComponent();

    try {
        JLabel l  =(JLabel) dtde.getTransferable().getTransferData(DataFlavor.stringFlavor);
        System.out.println("drop detected from "+l.getText()+" to "+this.getText());
    } catch (UnsupportedFlavorException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}


@Override
public void dropActionChanged(DropTargetDragEvent dtde) {
    // TODO Auto-generated method stub

}

that's how i initiale it:

    this.setLayout(null);
    NameSlot ns = new NameSlot("test");
    ns.setLocation(20, 20);
    this.add(ns);

    NameSlot ns2 = new NameSlot("test2");
    ns2.setLocation(20, 20);
    this.add(ns2);

after trying to use dtde.getSource() and scrapped it because of coolcfans comment, I now try to get the drag source using the Transferable of the DropTargetEvent. I get the exception:

java.awt.datatransfer.UnsupportedFlavorException: Unicode String
    at javax.swing.TransferHandler$PropertyTransferable.getTransferData(Unknown Source)
    at sun.awt.dnd.SunDropTargetContextPeer.getTransferData(Unknown Source)
    at sun.awt.datatransfer.TransferableProxy.getTransferData(Unknown Source)
    at java.awt.dnd.DropTargetContext$TransferableProxy.getTransferData(Unknown Source)
    at an.judosoft.view.bracketSheets.factory.NameSlot.drop(NameSlot.java:71)

Anyone knows the way?

Answers


The following works:

I changed this.setTransferHandler(new TransferHandler("foreground")) to this.setTransferHandler(new TransferHandler("text"))

and used new DataFlavor("application/x-java-jvm-local-objectref; class=java.lang.String") for the DataFlavor, though it returns a String instead of a JLabel. If you want a JLabel you'll need to change the TransferHandler.

import java.awt.FlowLayout;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
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.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.TransferHandler;

public class NameSlot extends JLabel implements DropTargetListener {
    NameSlot(final String name) {
        super(name);
        new DropTarget(this, this);
        this.setTransferHandler(new TransferHandler("text"));
        final MouseListener listener = new MouseAdapter() {
            @Override
            public void mousePressed(final MouseEvent me) {
                final JLabel comp = (JLabel) me.getSource();
                System.out.println(comp);

                final TransferHandler handler = comp.getTransferHandler();
                handler.exportAsDrag(comp, me, TransferHandler.COPY);
            }
        };
        this.addMouseListener(listener);
    }

    @Override
    public void dragEnter(final DropTargetDragEvent dtde) {
        // TODO Auto-generated method stub

    }

    @Override
    public void dragExit(final DropTargetEvent dte) {
        // TODO Auto-generated method stub

    }

    @Override
    public void dragOver(final DropTargetDragEvent dtde) {
        // TODO Auto-generated method stub

    }

    @Override
    public void drop(final DropTargetDropEvent dtde) {

        // DropTarget dt = (DropTarget) dtde.getSource();
        // NameSlot ns = (NameSlot) dt.getComponent();

        try {
            final String s = (String) dtde.getTransferable().getTransferData(
                    new DataFlavor("application/x-java-jvm-local-objectref; class=java.lang.String"));

            System.out.println("drop detected from " + s + " to " + this.getText());
        }
        catch (final UnsupportedFlavorException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (final IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (final ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    @Override
    public void dropActionChanged(final DropTargetDragEvent dtde) {
        // TODO Auto-generated method stub

    }

    public static void main(final String[] args) {
        final JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setLayout(new FlowLayout());

        final NameSlot ns = new NameSlot("test");
        frame.add(ns);

        final NameSlot ns2 = new NameSlot("test2");
        frame.add(ns2);

        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

    }
}

Need Your Help

browser size html css

iphone html css resize

I have been using the following css code:

Flags, loops and locks in async code

c# locking async-await

What I'm trying to do is create a 'Listener' which listens to several different Tcp ports at once, and pipes the messages to any Observers.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.