user interface - Drag and drop files in Matlab GUI -
i trying find way use drag , drop in matlab gui. closest i've found this.
however, result this:
when file has been dropped, need path of file , call load function.
all suggestions appreciated!
this submission maarten van der seijs on file exchange seems solve it.
it creates callback function can coupled java swing gui components, shown in attached demo.
it uses java class, thin wrapper around java.awt.dnd.droptarget
:
import java.awt.dnd.*; import java.awt.datatransfer.*; import java.util.*; import java.io.file; import java.io.ioexception; public class mldroptarget extends droptarget { /** * modified droptarget used drag & drop in matlab ui control. */ private static final long serialversionuid = 1l; private int droptype; private transferable t; private string[] transferdata; public static final int droperror = 0; public static final int droptexttype = 1; public static final int dropfiletype = 2; @suppresswarnings("unchecked") @override public synchronized void drop(droptargetdropevent evt) { // make sure drop accepted evt.acceptdrop(dndconstants.action_copy_or_move); // set droptype 0 droptype = droperror; // transferable , analyze t = evt.gettransferable(); try { if (t.isdataflavorsupported(dataflavor.javafilelistflavor)) { // interpret list of files list<file> filelist = (arraylist<file>) t.gettransferdata(dataflavor.javafilelistflavor); transferdata = new string[filelist.size()]; (int = 0; < filelist.size(); i++) transferdata[i] = filelist.get(i).getabsolutepath(); droptype = dropfiletype; } else if (t.isdataflavorsupported(dataflavor.stringflavor)) { // interpret string transferdata[0] = (string) t.gettransferdata(dataflavor.stringflavor); droptype = droptexttype; } } catch (unsupportedflavorexception e) { droptype = droperror; super.drop(evt); return; } catch (ioexception e) { droptype = droperror; super.drop(evt); return; } // call built-in drop method (fire matlab callback) super.drop(evt); } public int getdroptype() { return droptype; } public transferable gettransferable() { return t; } public string[] gettransferdata() { return transferdata; } }
which initialized , called matlab class:
classdef (caseinsensitiveproperties) dndcontrol < handle %dndcontrol class drag & drop functionality. % obj = dndcontrol(javaobj) creates dndcontrol object specified % java object, such 'javax.swing.jtextarea' or 'javax.swing.jlist'. 2 % callback functions available: obj.dropfilefcn , obj.dropstringfcn, % listen drop actions of respectively system files or plain text. % % drag & drop control class relies on java class need % visible on java classpath. initialize, call static method % dndcontrol.initjava(). java class can adjusted , recompiled if % desired. % % dndcontrol properties: % parent - associated java object. % dropfilefcn - callback function system files. % dropstringfcn - callback function plain text. % % dndcontrol methods: % dndcontrol - constructs dndcontrol object. % % dndcontrol static methods: % defaultdropfcn - default callback function drop events. % demo - runs demonstration script. % initjava - initializes java class. % isinitialized - checks if java class visible. % % demonstration available static method dndcontrol.demo(). % % example: % dndcontrol.initjava(); % dndcontrol.demo(); % % see also: % uicontrol, javaobjectedt. % % written by: maarten van der seijs, 2015. % version: 1.0, 13 october 2015. properties (hidden) droptarget; end properties (dependent) %parent associated java object. parent; end properties %dropfilefcn callback function executed upon dropping of system files. dropfilefcn; %dropstringfcn callback function executed upon dropping of plain text. dropstringfcn; end methods (static) function initjava() %initjava initializes required java class. %add java folder javaclasspath if necessary if ~dndcontrol.isinitialized(); classpath = fileparts(mfilename('fullpath')); javaclasspath(classpath); end end function tf = isinitialized() %isinitialized returns true if java class initialized. tf = (exist('mldroptarget','class') == 8); end end methods function obj = dndcontrol(parent,dropfilefcn,dropstringfcn) %dndcontrol drag & drop control constructor. % obj = dndcontrol(javaobj) contstructs dndcontrol object % given parent control javaobj. parent control should % subclass of java.awt.component, such java swing widgets. % % obj = dndcontrol(javaobj,dropfilefcn,dropstringfcn) sets % callback functions dropping of files , text. % check java class assert(dndcontrol.isinitialized(),'javaclass mldroptarget not found. call dndcontrol.initjava() initialization.') % construct droptarget obj.droptarget = handle(javaobjectedt('mldroptarget'),'callbackproperties'); set(obj.droptarget,'dropcallback',{@dndcontrol.dndcallback,obj}); set(obj.droptarget,'dragentercallback',{@dndcontrol.dndcallback,obj}); % set droptarget parent if nargin >=1, parent.setdroptarget(obj.droptarget); end % set callback functions if nargin >=2, obj.dropfilefcn = dropfilefcn; end if nargin >=3, obj.dropstringfcn = dropstringfcn; end end function set.parent(obj, parent) if isempty(parent) obj.droptarget.setcomponent([]); return end if isa(parent,'handle') && ismethod(parent,'java') parent = parent.java; end assert(isa(parent,'java.awt.component'),'parent not subclass of java.awt.component.') assert(ismethod(parent,'setdroptarget'),'droptarget cannot set on object.') obj.droptarget.setcomponent(parent); end function parent = get.parent(obj) parent = obj.droptarget.getcomponent(); end end methods (static, hidden = true) %% callback functions function dndcallback(jsource,jevent,obj) if jevent.isa('java.awt.dnd.droptargetdropevent') % drop event try switch jsource.getdroptype() case 0 % no success. case 1 % string dropped. string = char(jsource.gettransferdata()); if ~isempty(obj.dropstringfcn) evt = struct(); evt.droptype = 'string'; evt.data = string; feval(obj.dropstringfcn,obj,evt); end case 2 % file dropped. files = cell(jsource.gettransferdata()); if ~isempty(obj.dropfilefcn) evt = struct(); evt.droptype = 'file'; evt.data = files; feval(obj.dropfilefcn,obj,evt); end end % set dropcomplete jevent.dropcomplete(true); catch me % set dropcomplete jevent.dropcomplete(true); rethrow(me) end elseif jevent.isa('java.awt.dnd.droptargetdragevent') % drag event action = java.awt.dnd.dndconstants.action_copy; jevent.acceptdrag(action); end end end methods (static) function defaultdropfcn(src,evt) %defaultdropfcn default drop callback. % defaultdropfcn(src,evt) accepts following arguments: % src - dndcontrol object. % evt - structure fields 'droptype' , 'data'. fprintf('drop event %s component:\n',char(src.parent.class())); switch evt.droptype case 'file' fprintf('dropped files:\n'); n = 1:numel(evt.data) fprintf('%d %s\n',n,evt.data{n}); end case 'string' fprintf('dropped text:\n%s\n',evt.data); end end function [dndobj,hfig] = demo() %demo demonstration of dndcontrol class functionality. % dndcontrol.demo() runs demonstration. make sure % java class visible in java classpath. % initialize java class dndcontrol.initjava(); % create figure hfig = figure(); % create java swing jtextarea jtextarea = javaobjectedt('javax.swing.jtextarea', ... sprintf('drop files or text content here.\n\n')); % create java swing jscrollpane jscrollpane = javaobjectedt('javax.swing.jscrollpane', jtextarea); jscrollpane.setverticalscrollbarpolicy(jscrollpane.vertical_scrollbar_always); % add scrollpane figure [~,hcontainer] = javacomponent(jscrollpane,[],hfig); set(hcontainer,'units','normalized','position',[0 0 1 1]); % create dndcontrol jtextarea object dndobj = dndcontrol(jtextarea); % set drop callback functions dndobj.dropfilefcn = @demodropfcn; dndobj.dropstringfcn = @demodropfcn; % callback function function demodropfcn(~,evt) switch evt.droptype case 'file' jtextarea.append(sprintf('dropped files:\n')); n = 1:numel(evt.data) jtextarea.append(sprintf('%d %s\n',n,evt.data{n})); end case 'string' jtextarea.append(sprintf('dropped text:\n%s\n',evt.data)); end jtextarea.append(sprintf('\n')); end end end end
Comments
Post a Comment