File Exchange

image thumbnail

Drag & Drop functionality for JAVA GUI components

version 1.0 (4.63 KB) by

Class for Drag & Drop functionality



View License

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.

Comments and Ratings (19)


Meade (view profile)


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 file to the path. I will retry with just the path to the .class files.

Thanks again!

Yongjun Wang

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


Meade (view profile)


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?

Thanks again.


Dani (view profile)

Hi Andreas,

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).
Good luck!

Andreas Horn

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!

Koby Goldberg

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

Any ideas?


John (view profile)

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.

Joe Yeh

Joe Yeh (view profile)


Stav (view profile)

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!


Meade (view profile)


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.

Xiangrui Li

Dear Maaarten,
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.mouseMove(c(1), res(4)-c(2));
btn1= java.awt.event.InputEvent.BUTTON1_MASK;
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.

p fontaine

Excellent !

Xiangrui Li

Simply works. What can be better?

MATLAB Release
MATLAB 7.9 (R2009b)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Win prizes and improve your MATLAB skills

Play today