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

29
LINES

< > BotCompany Repo | #1024629 // jHandleMultiFileDrop - makes any JComponent accept multiple file drops (drag&drop)

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (1953L/13K).

1  
import java.awt.datatransfer.*;
2  
import java.awt.dnd.*;
3  
4  
// onDrop: voidfunc(L<File>), but may also return false
5  
static <A extends JComponent> A jHandleMultiFileDrop(A c, final O onDrop) {
6  
  new DropTarget(c, new DropTargetAdapter {
7  
    public void drop(DropTargetDropEvent e) {
8  
      pcall {
9  
        Transferable tr = e.getTransferable();
10  
        DataFlavor[] flavors = tr.getTransferDataFlavors();
11  
        for (DataFlavor flavor : flavors) {
12  
          if (flavor.isFlavorJavaFileListType()) {
13  
            e.acceptDrop(e.getDropAction());
14  
            L<File> files = cast tr.getTransferData(flavor);
15  
            if (nempty(files) && !isFalse(callF(onDrop, files)))
16  
              e.dropComplete(true);
17  
            ret;
18  
          }
19  
        }
20  
      }
21  
      e.rejectDrop();
22  
    }
23  
  });
24  
  ret c;
25  
}
26  
27  
static <A extends JComponent> A jHandleMultiFileDrop(O onDrop, A c) {
28  
  ret jHandleMultiFileDrop(c, onDrop);
29  
}

Author comment

Began life as a copy of #1006021

download  show line numbers  debug dex  old transpilations   

Travelled to 6 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1024629
Snippet name: jHandleMultiFileDrop - makes any JComponent accept multiple file drops (drag&drop)
Eternal ID of this version: #1024629/3
Text MD5: d803fb9a36ac5dd4bff45847aad7f47e
Transpilation MD5: 3f9d2c87f403bdeedf451ed13205f927
Author: stefan
Category: javax / gui
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-08-21 12:38:53
Source code size: 932 bytes / 29 lines
Pitched / IR pitched: No / No
Views / Downloads: 172 / 259
Version history: 2 change(s)
Referenced in: [show references]