gpt4 book ai didi

java - Matlab 将文件从 Windows 资源管理器拖放到图形 (gui)

转载 作者:太空宇宙 更新时间:2023-11-04 06:18:45 25 4
gpt4 key购买 nike

我想知道有一种方法可以从 Windows 资源管理器中拖动文件并将其放入我的 GUI 中。

目标应该是获取文件路径(或文件夹路径)并能够使用我自己的加载函数加载它。

我准确地说,我在 Windows 10 64 位中使用 Matlab 2015b。

我编辑我的帖子以给出我正在尝试做的事情的代码示例(基于 Yair Altman 解决方案和互联网上找到的其他解决方案):

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

我总是在该行中遇到错误:

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)

最佳答案

我尝试实现与您相同的功能,但在尝试获取可传输数据时遇到了相同的异常。

尚不清楚 getTransferable 是否由于 %matlabroot%\sys\java\jre\...\lib\flavormap.properties 中实例化的默认 FlavorMap 失败(如 Yair Altman's book 中所指出的)在拖放部分)或出于其他一些奇怪的原因。不管怎样,我遇到了这个dndcontrol对象 file exchange通过直接管理 java 端的可传输数据,这对我们的目的来说就像一个魅力。

我从中得到启发,在java.awt.dnd.DropTarget之上编写了我自己的matlab代理。它更通用,更接近其 java 实现对等体(即,它的工作方式与 java DropTarget 对象完全相同,只是所有数据类型都已转换为更标准和方便的 matlab 类型)。

您可以从这里下载我的实现:

这里有一些使用示例来完成您需要的操作(从文件资源管理器中放入 matlab 轴):

%
% 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 事件,以便您可以调整拖动操作的各个方面。不费吹灰之力,它还可以扩展为支持图像拖动或其他数据类型的拖动。

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

关于java - Matlab 将文件从 Windows 资源管理器拖放到图形 (gui),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27720121/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com