- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我想知道有一种方法可以从 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
该对象还支持捕获 DragEnter
、DragOver
、DropActionChanged
、DragExit
事件,以便您可以调整拖动操作的各个方面。不费吹灰之力,它还可以扩展为支持图像拖动或其他数据类型的拖动。
希望您会喜欢它,并且您会发现它足够通用,可以考虑其他用途。
关于java - Matlab 将文件从 Windows 资源管理器拖放到图形 (gui),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27720121/
我有一个 12 节点集群,我运行的是 yarn 架构。似乎我的节点大部分时间都很忙,而且很多时候作业都失败了。如何随时查看资源的使用情况?还有什么方法可以为用户设置有限的资源,例如:如果用户提交作业,
我有一个关于 Xtend 的非常具体的问题。 在我读到的有关 xText/xTend 的每个示例中,我都会看到类似这样的内容: override void doGenerate(Resource re
代码参考 代码文件参考下述详解的类图,工程参考第零章工程说明 。 概述 在游戏项目中有很多资产如:预制体,图片,音频,Lua脚本,Shader等等。他们随打包放在用户的硬盘里。在游戏的运
我有一个包含10个节点的Hadoop集群。在10个节点中,有3个部署了HBase。有两个共享集群的应用程序。 应用程序1从hadoop HDF写入和读取数据。应用程序2将数据存储到HBase中。有没有
例子: var image = UIImage.FromFile("/path/to/image.png"); var imageView = new UIImageView(); imageView
我需要存储 ThirdPartyElm 类型的元素序列,我使用的是 std::vector(或 std::array 如果我需要一个固定大小的序列)。 我想知道我应该如何初始化序列。第一个版本创建一个
我有一个具有UITabBarController的MonoTouch应用程序,每个选项卡都是一个UINavigationController。其中一些包装了一个UIViewController,后者添
所以我一直在研究使用资源管理 API 对 azure 进行只读 api 访问。现在我主要关注虚拟机。我一直在使用这个预发布包和 TokenCredentials: https://www.nuget.
我想获取 Azure 中包含给定标签的资源列表。基本请求形式如下:https://management.azure.com/subscriptions/{subscriptionId}/resourc
我一直在尝试访问 Azure 公开的 REST API,例如“https://management.azure.com/subscriptions?api-version=2016-06-01”,但无
如何使用 Azure 资源管理 API 获取资源组的资源列表 我已经安装了 Microsoft.Azure.Management.ResourceManager.Fluent Nuget 包下面的脚本
如何使用 Azure 资源管理 API 获取资源组的资源列表 我已经安装了 Microsoft.Azure.Management.ResourceManager.Fluent Nuget 包下面的脚本
我正在尝试在 Jersey 中编写 RESTful Web 应用程序时管理竞争资源(如:数据库 session )。通常我会写这样的代码: Session session = getSession()
我是这个论坛的新手。 我正在查看有关 Azure 资源运行状况 Rest Api 的 Microsoft 文档,链接如下: https://azure.microsoft.com/en-us/blog
我想使用这些 API 获取特定 Azure 帐户或订阅的运行状况和其他资源详细信息 https://learn.microsoft.com/en-us/rest/api/resourcehealth/
我正在尝试在 Azure 资源管理的上下文中找到应用程序权限的安全最佳实践。 目前,management.azure.com 仅列出了一项权限,它是management.azure.com/user_
我想使用这些 API 获取特定 Azure 帐户或订阅的运行状况和其他资源详细信息 https://learn.microsoft.com/en-us/rest/api/resourcehealth/
我正在尝试在 Azure 资源管理的上下文中找到应用程序权限的安全最佳实践。 目前,management.azure.com 仅列出了一项权限,它是management.azure.com/user_
我正在寻找一种以编程方式列出组织内的 Google Cloud 项目的方法。我正在尝试使用服务帐户导出的 json 凭据以这种方式实现此目的: // More info on the endp
我正在编写一些用于自动部署Azure网站的代码(包括在Azure中创建网站)。我正在使用 Nuget 中提供的 Azure 管理库和 Azure 资源管理库。大部分内容都已到位,但是我无法找到通过我见
我是一名优秀的程序员,十分优秀!