Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Drag and Drop Examples Anywhere



At 5:59 -0800 16/11/02, "Michael Purdy" <email@hidden> wrote:
>
>I'm searching for any drag and drop example code for dragging something
>from the OSX finder to my Java Application.
>
>In particular, I just want to be able to drag a file or alias to my
>application and I want to extract the file's path and display it.
>That's it. Anyone know where I can find some examples?

That's good timing, I only figured out how to do it myself yesterday!

Register a DropTargetListener on a component. That component must be the
active, topmost component. Figuring out *which* component was the hardest
part! In my case, that meant the viewport of the scroll pane that filled
my window.

new DropTarget(component, DnDConstants.ACTION_COPY_OR_MOVE, listener);

Your DropTargetListener has the following:

public void drop(DropTargetDropEvent ev)
{
try
{
Transferable trans = ev.getTransferable();
if (ev.isDataFlavorSupported(DataFlavor.javaFileListFlavor))
{
ev.acceptDrop(DnDConstants.ACTION_COPY);
List fileList = (List)trans.getTransferData(DataFlavor.javaFileListFlavor);
Iterator iter = fileList.iterator();
while( iter.hasNext() )
{
File file = (File) iter.next();
System.out.println("Drop "+file);
}
ev.dropComplete(true);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}

You might also want to process dragEnter and dragExit to draw a border
or something round your component to provide visual feedback to the user.

-Rolf
--
Rolf Howarth, Square Box Systems Ltd, Stratford-upon-Avon UK.
_______________________________________________
java-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/java-dev
Be sure to read the FAQ http://developer.apple.com/java/faq/ before posting
Do not post admin requests to the list. They will be ignored.



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.