Matlab将文件从Windows资源管理器拖放到图形(GUI) [英] Matlab drag and drop file from windows explorer to figure (gui)

查看:86
本文介绍了Matlab将文件从Windows资源管理器拖放到图形(GUI)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道有一种方法可以将文件从Windows资源管理器中拖放到我的GUI中.

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.

我确定我在Windows 10 64位系统中使用Matlab 2015b.

I precise that I am using Matlab 2015b in Windows 10 64bits.

我编辑我的帖子以给出我要做什么的代码示例(基于Yair Altman解决方案和Internet上的其他解决方案):

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); 

错误如下:

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)

推荐答案

我尝试实现与您相同的功能,并在尝试获取可传输数据时遇到了相同的例外.

I tried to implement same functionality as yours and fall into the same exceptions when trying to get transferable data.

尚不清楚getTransferable是否由于在%matlabroot%\sys\java\jre\...\lib\flavormap.properties 中实例化的默认FlavorMap而失败(如 Yair Altman的书 在拖放部分)或其他一些奇怪的原因.无论如何,我遇到了这个 dndcontrol 文件交换上的对象,它通过管理可转让对象而像我们的魅力数据直接在Java端.

It is rather unclear if getTransferable fails because of default FlavorMap instantiated in %matlabroot%\sys\java\jre\...\lib\flavormap.properties (as pointed in Yair Altman's book in drag and drop section) or for some other strange reason. Anyway I came accross this dndcontrol object on file exchange which works like a charm for our purpose by managing transferable data on java side directly.

我从中得到启发,在 java.awt.dnd.DropTarget ,它更通用并且更接近其Java实现对等体(即,它与Java DropTarget对象的工作方式完全相同,只是所有数据类型都已转换为更标准,更方便的matlab类型.

I get inspired from this to wrote my own matlab proxy on top of java.awt.dnd.DropTarget that is more generic and closer to its java implementation peer (i.e. it works exactly the same way as java DropTarget object except that all data types have been converted to more standard and convenient matlab types).

您可以从此处下载我的实现:

You can download my implementation from here:

  • GitHub: DropListener
  • FileExchange: DropListener

这是一些您需要做的用法示例(从文件资源管理器中放入matlab轴):

And here is some usage example for doing what you need (drop in matlab axis from file explorer):

%
% PURPOSE:
%
%   Show how to add drop support from file explorer to some matlab axis
%
% SYNTAX:
%
%   [] = DropListenerDemo();
%
% USAGE:
%
%   Simply drop files from file explorer into displayed axis.
%

%%
function [] = DropListenerDemo()
%[
    % Create a figure with some axis inside
    fig = figure(666); clf;
    axes('Parent', fig);

    % Get back the java component associated to the axis
    % NB1: See §3.7.2 of Undocumented Secrets of Matlab Java Programming
    % NB2: or use findjobj, or javaObjectEDT for drop support onto other component types
    jFrame = get(handle(fig), 'JavaFrame');
    jAxis = jFrame.getAxisComponent();

    % Add listener for drop operations
    DropListener(jAxis, ... % The component to be observed
                 'DropFcn', @(s, e)onDrop(fig, s, e)); % Function to call on drop operation    
%]
end
function [] = onDrop(fig, listener, evtArg) %#ok<INUSL>
%[
    % Get back the dropped data
    data = evtArg.GetTransferableData();

    % Is it transferable as a list of files
    if (data.IsTransferableAsFileList)       

        % Do whatever you need with this list of files
        msg = sprintf('%s\n', data.TransferAsFileList{:});
        msg = sprintf('Do whatever you need with:\n\n%s', msg);
        uiwait(msgbox(msg));

        % Indicate to the source that drop has completed 
        evtArg.DropComplete(true);

    elseif (data.IsTransferableAsString)

        % Not interested
        evtArg.DropComplete(false);

    else

        % Not interested
        evtArg.DropComplete(false);

    end
%]
end

该对象还支持捕获DragEnterDragOverDropActionChangedDragExit事件,因此您可以调整拖动操作的各个方面.毫不费力地将其扩展为支持图像拖动或其他数据类型拖动.

The object also support for catching DragEnter, DragOver, DropActionChanged, DragExit event so you can tune every aspects of dragging operation. With little effort, it can also be extended to support for image dragging or other data types dragging.

希望您会喜欢它,并且发现它足够通用,可以考虑其他用法.

Hope you'll like it and you'll find it generic enough to think of other usages.

这篇关于Matlab将文件从Windows资源管理器拖放到图形(GUI)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆