We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I am currently working on a project where if a user drags a file onto the program's screen, it will take in the path and use its location to create a shortcut to it inside the program. I have heard the term for this as "drag-and-drop" functionality and have seen it in many popular programs and editing softwares. I am wondering if it is at all possible for me to be able to drag a program or file onto the screen and take its path in a variable. I am not looking for the ability to drag and drop a file onto my sketches icon (I have already done this through file-type association). I have also heard the terms "stdin" and "stdout" and they sounded similar to how I might achieve this or possible the way I need to approach this, but I am unsure about how to recreate that in processing.
Thanks in advance,
Matt
Answers
Processing's IDE allows drag 'n' drop for image files. I just dunno which source file that feature belongs to! :-\"
There was a library offering several D'n'D facilities, including system ones. I don't know if it is still available. It is a thin wrapper around Java's capabilities (clipboard).
This sounded interesting so I went on investigating.
-The SDrop library is outdated...- SDrop still works fine! It wasn't on the official library page so I assumed it was outdated.
https://github.com/processing/processing/blob/258dd07e0aaf87525d73d421844be61dd0144eaf/app/src/processing/app/Editor.java
Lines 315-371 code a subclass (FileDropHandler) which extends TransferHandler, which is a Java class. In line 249, the
JPanel pain
is assigned a new FileDropHandler.This is a dead end. the PDE uses a JPanel inside a JFrame (I guess) but a PApplet uses a Frame instead of a JFrame! So the TransferHandler thing won't work. This is because according to the PApplet Javadoc, "This class extends Applet instead of JApplet because 1) historically, we supported Java 1.1, which does not include Swing (without an additional, sizable, download), and 2) Swing is a bloated piece of crap. " So there's that.
I think the solution can be found here: http://docs.oracle.com/javase/7/docs/api/java/awt/dnd/package-summary.html but haven't looked into it very hard yet. It's surprisingly hard to find a Java tutorial about drag and drop which does not depend on Swing components.
That's not exactly true! PApplet actually instantiates a JFrame, but stores its reference in a Frame variable: :-B
https://github.com/processing/processing/blob/master/core/src/processing/core/PApplet.java#L10729
public Frame frame;
Frame frame = new JFrame(displayDevice.getDefaultConfiguration());
applet.frame = frame;
Good catch. I tried it out but it didn't work. I made a similar FileDropHandler class which extended TransferHandler in the same way as the PDE source code, but I got an error when I tried this:
frame.setTransferHandler(new FileDropHandler());
in the setup(): "Error: function setTransferHandler(FileDropHandler) does not exist". That's how I discovered a PApplet uses a Frame instead of a JFrame (or so I thought). A Frame doesn't have the setTransferHandler() function, but a JFrame does. So what is aPApplet frame
, a Frame or a JFrame? :-/I'm looking at the source code of SDrop in hopes of finding the solution there.
By the way, SDrop works fine in Processing 2.2.1.
There are a number of limitations to SDrop. You can only drop into the program, not out of the program. You can also only drop one file at a time. I'll try to find a solution for that.
frame is just a variable. What matters is the reference stored in it! That reference points to a JFrame object.
But since frame was declared as Frame, object's members are constrained to those present in that class only!
In order to access all JFrame's class members, we gotta use (cast) on the variable:
((JFrame) frame).setTransferHandler(new FileDropHandler());
Or then declare another variable w/ (cast) already applied on it: