I would like to know it there is a way to drag a file from Windows explorer and drop it in my GUI.
The goal should be to get the file path (or folder path) and be able to load it with my own loading function.
I precise that I am using Matlab 2015b in Windows 10 64bits.
I edit my post to give an code example of what I am trying to do (based on Yair Altman solution and other found in Internet) :
function demo
% Set-up a figure droppable axis
hFig = figure('name','DND example','numbertitle','off');
hAx1 = axes('position',[.1,.1,.8,.8]);
% Enable drop on the figure axis
dnd = handle(java.awt.dnd.DropTarget(),'callbackProperties');
jFrame = get(hFig,'JavaFrame');
jAxis = jFrame.getAxisComponent;
jAxis.setDropTarget(dnd);
set(dnd,'DropCallback',{@dndCallbackFcn,hFig, hAx1});
set(dnd,'DragOverCallback',@dndCallbackFcn);
end
function dndCallbackFcn(varargin)
persistent transferable
eventData = varargin{2};
if eventData.isa('java.awt.dnd.DropTargetDropEvent') %nargin>2
hFig = varargin{3}; % my figure is passed as the third argument
try
eventData.acceptDrop(eventData.getDropAction);
transferable = eventData.getTransferable;
catch
end
dataFlavorList = java.awt.datatransfer.DataFlavor.javaFileListFlavor;
fileList = transferable.getTransferData(dataFlavorList);
%{
I want here to get back the file path and then call my loading function
%}
end
end
I always get an error in the line :
fileList = transferable.getTransferData(dataFlavorList);
The error is the following :
Java exception occurred:
java.awt.dnd.InvalidDnDOperationException: No drop current
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)