obj = dndcontrol(javaobj) creates a dndcontrol object for the specified Java object, such as 'javax.swing.JTextArea' or 'javax.swing.JList'. Two callback functions are available: obj.DropFileFcn and obj.DropStringFcn, that listen to drop actions of respectively system files or plain text.
The Drag & Drop control class relies on a Java class "MLDropTarget.class" that need to be visible on the Java classpath. To initialize, call the static method dndcontrol.initJava(). The Java class can be adjusted and recompiled if desired.
A demonstration is available from the static method dndcontrol.demo().
Written by: Maarten van der Seijs, 2015.
Maarten van der Seijs (2021). Drag & Drop functionality for JAVA GUI components (https://www.mathworks.com/matlabcentral/fileexchange/53511-drag-drop-functionality-for-java-gui-components), MATLAB Central File Exchange. Retrieved .
I can confirm with @Meade that changing the javaclasspath to javaaddpath is the way to go if you use other javaclass in conjunction with dndcontrol
After a couple months of on-again/off-again debugging, I think I have ID'd the problem with my deployed application.
I am using a number of other custom Java classes. I add these to the dynamic java path just before use. However, the dndcontrol.initJava() call includes the following syntax:
javaclasspath(classpath) % Ln62 This reassigns the java dynamic path, instead of appending it. Any previously added java paths are lost.
Perhaps this could be changed to:
Thanks again for your insight, I was able to use the code you suggested previously to chase this down.
how can i make the scrollbar move and update to the bottom after the last item dropped. right now it doesn't move unless I manually slide it.
Thanks so much for the insight. I'm actually already using that same technique to feed the |javaaddpath| function, but I've been adding the whole dndcontrol.zip file to the path. I will retry with just the path to the .class files.
Thank you ,good job!
@Meade: I cannot pinpoint the exact reason for this, but what I've learnt about using custom java classes in compiled (deployed) versions of your software is that:
1) there is no real difference so in principle it should be able to work;
2) the working directory of deployed apps is typically not the directory of the .exe, but some temporary folder where the .exe content is extracted to. So it could be that you have the same problem. There are probably better solutions, but a quick hack would be to do the following:
function p = getExePath()
if isdeployed % Stand-alone mode.
[~, result] = system('set PATH');
p = char(regexpi(result, 'Path=(.*?);', 'tokens', 'once'));
else % Running from MATLAB.
p = fileparts(mfilename('fullpath'));
Then construct the full path of the java class (fullfile(p,'your_relative_location...')) and add it using javaaddpath() on initialisation of your programme.
A bit of a hack as you can see, but this works 100% of the time for me in an application that also runs in corporate (admin-restricted) environments.
Hope it works for you too! Best, Maarten
I've been using your java class for a couple years now and rely on it daily.
However, I recently packaged a gui I built as an *.exe, and the drag and drop ceased to work. I suspect it has something to do with how the javapath is redefined when its compiled.
Do you have any thoughts on how to use your custom class in an executable?
Yes you can! You might want to check out the tool from Yair Altman to do excactly this:
A quick solution which works on recent MATLAB versions is:
function [jFrame, jContentPane] = getJavaFrame(hFig)
wrn = warning('off','MATLAB:HandleGraphics:ObsoletedProperty:JavaFrame');
jFrame = get(hFig,'JavaFrame');
jContentPane = jFrame.fHG2Client().getContentPane();
You might want to add this function to the static functions of the droptarget class.
You can then use the jContentpane object, which allows to set a drop action to the whole figure (as far as I know).
Hi there & thank you for this great submission!
Is it possible to receive dnd calls for a whole figure? I don't know how to get the java handle from the Matlab fig handle..
Thanks so much!
I found how to change the font. If someone else needs it: add this line and set your desired font:
Amazing! Works great!
Is it possible to change the Font?
I've tried this, but it didn't change the font:
jScrollPane.setFont(java.awt.Font('monospaced', java.awt.Font.PLAIN, 12))
Truly a work of art. I have been searching for a cross-platform solution for this for so long. Thank you sincerely Maarten. This needs to be shared with many others. All the best to you.
Fantastic work. I really recommend you do a quick search on Google for "Drag and drop in MATLAB" and post a link to your contribution on all the forums this question was asked. It took me a long while to find this and it's a shame since it does a great job at a non-trivial task. Thanks again for sharing your work!
This is just fantastic! I'm barely a java beginner, but after a short time I was able to incorporate this into my GUI.
Thanks so much.
I am using dndcontrol for a figure to receive dnd. It works very well. I was thinking to distinguish two kinds of drops: with and without holding Ctrl key during the drop. The figure's key events are not reliable since it caches previous event and it has possibly focus issue during the drop (focus may be on file browser).
Do you think there is a simple way to tell whether Ctrl key is down inside drop callback?
I am now using following method to do this, but for unknown reason it is not reliable:
fh = gcf; drawnow; % dnd figure front
pos = get(fh, 'Position');
c = pos(1:2) + round(pos(3:4)/2); %center
res = get(0, 'ScreenSize');
robot = java.awt.Robot;
robot.mousePress(btn1); drawnow; pause(0.1); robot.mouseRelease(btn1); % robot click
disp(get(fh, 'SelectionType')) % alt if Ctrl down
The idea is to bring dnd figure front, then robot clicks at somewhere in the figure to set SelectionType to 'alt' if Ctrl is down.
It works most of time, but sometimes it fails to report correct Ctrl key status.
I appreciate any suggestion on this. Thanks.
Simply works. What can be better?
Inspired by: UICOMPONENT - expands uicontrol to all Java classes
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!