0

As you can read here there is no drop already implemented for a JList because there are many possibilities how to treat it. In my case I just need to know the String which was dropped. I don't bother to know the location. Is this possible with a few code lines or do I have to go into depth?

principal-ideal-domain
  • 3,998
  • 8
  • 36
  • 73
  • [Did you check this link](https://docs.oracle.com/javase/tutorial/uiswing/dnd/dropmodedemo.html) – Yassin Hajaj Dec 12 '15 at 22:36
  • For [example](http://stackoverflow.com/questions/13855184/drag-and-drop-custom-object-from-jlist-into-jlabel/13856193#13856193) and [example](http://stackoverflow.com/questions/15531783/add-string-to-jlist-exactly-where-theyre-dropped-not-at-the-bottom/15536464#15536464) – MadProgrammer Dec 12 '15 at 22:44

1 Answers1

0

Thanks to the comments. Here is what I made out of it:

jList.setTransferHandler(new TransferHandler() {
    private static final long serialVersionUID = -7001848419493253013L;
    @Override
    public boolean canImport(TransferHandler.TransferSupport info) {
        if (!info.isDataFlavorSupported(DataFlavor.stringFlavor)) {
            return false;
        }
        return true;
    }
    @Override
    public boolean importData(TransferSupport info) {
        Transferable t = info.getTransferable();
        String data;
        try {
            data = (String) t.getTransferData(DataFlavor.stringFlavor);
            System.out.println(data);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
});
principal-ideal-domain
  • 3,998
  • 8
  • 36
  • 73