Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

29
LINES

< > BotCompany Repo | #1006021 - jHandleFileDrop - makes any JComponent accept file drops (drag&drop)

JavaX fragment (include)

import java.awt.datatransfer.*;
import java.awt.dnd.*;

// onDrop: voidfunc(File), but may also return false
static <A extends JComponent> A jHandleFileDrop(A c, final O onDrop) {
  new DropTarget(c, new DropTargetAdapter {
    public void drop(DropTargetDropEvent e) {
      pcall {
        Transferable tr = e.getTransferable();
        DataFlavor[] flavors = tr.getTransferDataFlavors();
        for (DataFlavor flavor : flavors) {
          if (flavor.isFlavorJavaFileListType()) {
            e.acceptDrop(e.getDropAction());
            File file = first((L<File>) tr.getTransferData(flavor));
            if (file != null && !isFalse(callF(onDrop, file)))
              e.dropComplete(true);
            ret;
          }
        }
      }
      e.rejectDrop();
    }
  });
  ret c;
}

static <A extends JComponent> A jHandleFileDrop(O onDrop, A c) {
  ret jHandleFileDrop(c, onDrop);
}

Author comment

Began life as a copy of #1006013

download  show line numbers  debug dex  old transpilations   

Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, sawdedvomwva, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1006021
Snippet name: jHandleFileDrop - makes any JComponent accept file drops (drag&drop)
Eternal ID of this version: #1006021/5
Text MD5: fd3a81d39244efa49c293793433fd422
Author: stefan
Category: javax / gui
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-12-08 15:52:49
Source code size: 920 bytes / 29 lines
Pitched / IR pitched: No / No
Views / Downloads: 400 / 399
Version history: 4 change(s)
Referenced in: [show references]