Drag and drop files

Hello,
I've been working with Swing for a while now and it seems to work out perfectly well, only there is one thing I just don't understand. I have read numerous tutorials on this subject but wasn't able to find the current situation.
For example:
I have a JTable, populated with rows and columns. I want the user to be able to drag a row, to a folder somewhere on his computer, lets say desktop. I think the following needs to happen:
User begins dragging of the selected row
User drops it on the desktop
Desktop somehow magically returns the path where it was dropped
and some function, which I define, does the rest
What do I need to do in general to get this working?
Thanks

Ok, I have written the code with help of the tutorial, though, this is what I expected that would happen.
* ZFileTable.java
* Created: 14 may 2007
* Author: Lars 'Levia' Wesselius
* Description: The list for showing the files in the opened zip archive.
package Zipper;
import java.awt.*;
import java.io.*;
import java.util.*;
import java.awt.event.*;
import java.awt.dnd.*;
import java.awt.datatransfer.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
public class ZFileTable extends JTable
                        implements DropTargetListener, DragSourceListener, DragGestureListener
    private ZControls m_Controls;
    DropTarget dropTarget = new DropTarget(this, this);
    DragSource dragSource = DragSource.getDefaultDragSource();
    public ZFileTable(DefaultTableModel model, ZControls controls)
        m_Controls = controls;
        dragSource.createDefaultDragGestureRecognizer(this, DnDConstants.ACTION_COPY_OR_MOVE, this);
        setModel(model);
    /* Method fired when something is dropped upon this table. */
    public synchronized void drop(DropTargetDropEvent dropTargetDropEvent)
        System.out.println("drop");
        try
            Transferable transferable = dropTargetDropEvent.getTransferable();
            if (transferable.isDataFlavorSupported(DataFlavor.javaFileListFlavor))
                dropTargetDropEvent.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
                java.util.List fileList = (java.util.List)transferable.getTransferData(DataFlavor.javaFileListFlavor);
                Iterator iterator = fileList.iterator();
                if (fileList.size() != 1)
                    dropTargetDropEvent.rejectDrop();
                    return;
                while (iterator.hasNext())
                    File file = (File)iterator.next();
                    m_Controls.doArchive(file.getCanonicalPath());
                dropTargetDropEvent.getDropTargetContext().dropComplete(true);
            else
                dropTargetDropEvent.rejectDrop();
        catch (IOException io)
        catch (UnsupportedFlavorException ufe)
    public void dragOver(DropTargetDragEvent dropTargetDragEvent)
        System.out.println("DragOver");
    public void dragExit(DropTargetEvent dropTargetEvent)
        System.out.println("DragExit");
    public void dropActionChanged(DropTargetDragEvent dropTargetDragEvent)
        System.out.println("DropActionChanged");
    public void dragEnter(DropTargetDragEvent dropTargetDragEvent)
        System.out.println("DragEnter");
        dropTargetDragEvent.acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE);       
    public void dragExit(DragSourceEvent dragSourceEvent)
        System.out.println("DragExit");
    public void dropActionChanged(DragSourceDragEvent dragSourceDragEvent)
        System.out.println("dropActionChanged");
    public void dragOver(DragSourceDragEvent dragSourceDragEvent)
        System.out.println("DragOver");
    public void dragDropEnd(DragSourceDropEvent dragSourceDropEvent)
        System.out.println("DragdropEnd");
    public void dragEnter(DragSourceDragEvent dragSourceDragEvent)
        System.out.println("DragEnter");
    public void dragGestureRecognized(DragGestureEvent dragGestureEvent)
        System.out.println("DragGestureRecognized");
        // Get the value we want to transfer
        String str = (String)getValueAt(getSelectedRow(), getSelectedColumn());
        // Check if the value is not null
        if (str != null)
            ZTransferable transferable = new ZTransferable(str);
            dragGestureEvent.startDrag(DragSource.DefaultCopyDrop, transferable, this);
    public Class getColumnClass(int column)
        return getValueAt(0, column).getClass();
    public boolean isCellEditable(int rowIndex, int vColIndex)
        return false;
}It currently accepts dropping and that works fine. Only when I drag an item from the table, and drop it on the desktop, I get an error..I suppose it should be changed and handled in the transferable. When I drag it into a text editor, I get to see the string, which is right, which comes from the transferable.
* ZTransferable.java
* Created: 14 may 2007
* Author: Lars 'Levia' Wesselius
* Description: This class is going to transfer data via DnD.
package Zipper;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.datatransfer.Transferable;
import java.awt.dnd.*;
import javax.swing.*;
import java.io.IOException;
import java.io.File;
import java.util.Vector;
class ZTransferable extends Vector implements Transferable
    DataFlavor flavors[] = {
                            DataFlavor.javaFileListFlavor,
                            DataFlavor.stringFlavor
    public ZTransferable(String str)
        addElement(str);
    public synchronized DataFlavor[] getTransferDataFlavors()
         return flavors;
    public boolean isDataFlavorSupported(DataFlavor flavor)
        boolean isSupported = false;
        isSupported |= flavor.equals(flavors[0]);
        isSupported |= flavor.equals(flavors[1]);
        return isSupported;
    public synchronized Object getTransferData(DataFlavor flavor)
                   throws UnsupportedFlavorException, IOException
        if (flavor.equals(flavors[0]))
             return this;
         else if (flavor.equals(flavors[1]))
            return elementAt(0);
        else
            throw new UnsupportedFlavorException(flavor);
}What do I need to change?

Similar Messages

  • I have 2 auxillary disk drives for my MacBokk Pro. Time Machine uses one for backup and I use the other for storage, but I am unable to create a new folder or drag and drop files or folders to the aux drive.

    I am unable to create a new folder on either of my auxillary disk drives.  I can access the files and folders that were there when I transferred one of the drives from a PC.  Time machine uses one for backup and I want to use the other to store photos which are large files.  An help would be appreciated.

    Thanks for the reply.  If I reformat the drive will I be able to access the files on it from the MAC.  I do not share the drive with a PC.  The files were originally created on a PC which I no longer use.  I do want to continue to access the files put there by the PC.  I would like to not have to copy them over to the MAC hard drive just to use them.  The second aux drive is new and works fine because it was formatted by the MAC.  I can drag and drop files there and work with just as if they were on the internal drive.

  • HT2518 how do i or can i drag and drop files from one external drive to another ???HOW

    how do i drag and drop files from one external drive to another, i cannt get both drives to open up. only one at a time.. i have alot to learn i know but that is where i am at the time...

    You posted here so would assume you are also talking about running Windows.
    Are the drives NTFS and use on Windows?
    Are they Mac HFS formatted?
    You likely will need NTFS for OS X in one case. to mount PC and use under Mac OS Lion
    MacDrive to mount and WRITE to HFS drives under Windows

  • Drag and drop files into folders in iPad reader? or iTunes?

    is it possible to drag and drop files into folders in iPad reader? or iTunes?  It is very clunkey to have to click each separate file to instruct it to move to the folder.  Better still, can I just sync an existing folder on my hard drive so the pdf's inside always remain up to date on my iPad?

    There is no way to drag and drop in Reader on an iPad.
    There is no way to sync PDFs in a folder on your hard drive to those inside of Reader.
    There are readers (which are NOT free) which let you do some of these things and have more capabilities. You might check out PDF Expert from Readdle which costs $9.99 US.

  • I can't drag and drop files...

    Hello!
         Out of nowhere, I can no longer drag and drop files on my desktop or within my Finder windows.  I also cannot drag pictures from Safari onto my Desktop.  I do not think there is a problem with my Trackpad as I can still move Safari windows and the like around the screen.  I am using an Intel-based Mac Book Pro with OS X 10.5.8  Any help would be amazing and much appreciated.  Thank you!

    I'm having the same issue.  I can no longer drag images from Safari (5.1.2) to Finder.  I am on Lion (10.7.2).  Has anybody found a fix for this?  Having to right-click and "Save As..." makes me feel like I'm working in Windows XP, which is, needless to say, he11 of an unbearable kind.

  • Drag and dropping files into widgets

    I just installed two widgets that are supposed to function by dragging and dropping files into them. How do I get access to files? How do you get files from the Finder into the dashboard?

    EMichaelM wrote:
    I just installed two widgets that are supposed to function by dragging and dropping files into them. How do I get access to files? How do you get files from the Finder into the dashboard?
    You need to activate Dashboard's "Developer Mode" in order to keep widgets active on the desktop.
    Open Terminal and enter the following command:
    <PRE>
    defaults write com.apple.dashboard devmode YES
    </PRE>
    Close Terminal, log out of your user account, and log back in again. To move a widget onto your desktop, activate Dashboard and start dragging the widget to the desktop. Press F12 before letting up on the mouse button to close Dashboard, and the widget should now be permanently affixed to the desktop. Drag to your desired location.
    Steve

  • Unable to drag and drop files or folders

    Hi
    I'm unable to drag and drop files or folders. I can't drag from a folder to desktop, I can't drag from finder. I can't move files around. I can't drag from itunes into ipod.
    I've run Onyx and disk permission repair, cleaned all caches etc, all to no avail.
    Get info tells me I have read and write permissions.
    Can someone please help me.
    Thanks

    I'm having the same problem, the terminal command to make that testfile worked, guess its a good sign but i wouldn't know, I also have a couple of extra problems:
    It started with VisualHub saying "Critical componats of Mac OS X are missing or damaged on your system, Without them, VisualHub is unable to continue. The affected componats is/are: - /tmp Temporary Directory. Try Repairing Permissions using Disk Utility"
    So i did that using OnyX, rebooted, still didn't work, so i remember hearing about how reinstalling .3 of 10.5.3 can help replace files that are lost, so i did that and thats where it went wrong, it rebooted and it seemed to go good, but then i noticed a big ? where Camino is meant to be on my Dock and i looked in my stack and it was gone, Safari would crash on the 5th letter i typed anywhere on the browser, (Safari and Webkit Nightly) At least Firefox works so i can post this, Spotlight is gone from the menu bar, i can't drag anything from the dock and i can't drag safari from the App stack or the dock crashes.
    The thing that worries me is why was Camino deleted? and Safari crashing?
    If anyone has a solution to this you would be a HUGE help, i really don't want to reformat my mac because i don't have anywhere to backup my files, i don't have the money for a decent sized HDD to backup into.
    Thanks all who help!!!

  • Can't drag and drop files into Attachments while in Administrator mode

    I can drag and drop files into attachment easily when I'm not in Administrator mode. But I need to be in Administrator mode at work and therefore for some reason I cannot drag and drop files into attachments; I'm forced to manually search and select particular files for attaching, which significantly slows my day-to-day work.
    Is it a bug?
    I have the up-to-date version of Thunderbird and I use MS Windows 7 Ultimate.
    Any help would be highly appreciated.
    Thx

    To diagnose problems with Thunderbird, try one of the following:
    *Restart Thunderbird with add-ons disabled (Thunderbird Safe Mode). On the Help menu, click on "Restart with Add-ons Disabled". If Thunderbird works like normal, there is an Add-on or Theme interfering with normal operations. You will need to re-enable add-ons one at a time until you locate the offender.
    *Restart the operating system in '''[http://en.wikipedia.org/wiki/Safe_mode safe mode with Networking]'''. This loads only the very basics needed to start your computer while enabling an Internet connection. Click on your operating system for instructions on how to start in safe mode: [http://windows.microsoft.com/en-us/windows-8/windows-startup-settings-including-safe-mode Windows 8], [http://windows.microsoft.com/en-us/windows/start-computer-safe-mode#start-computer-safe-mode=windows-7 Windows 7], [http://windows.microsoft.com/en-us/windows/start-computer-safe-mode#start-computer-safe-mode=windows-vista Windows Vista], [http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/boot_failsafe.mspx?mfr=true" Windows XP], [http://support.apple.com/kb/ht1564 OSX]
    ; If safe mode for the operating system fixes the issue, there's other software in your computer that's causing problems. Possibilities include but not limited to: AV scanning, virus/malware, background downloads such as program updates.

  • Can't drag and drop files onto Applications in Dock since upgrading to 10.6

    Ever since my wife upgraded her iMac (2 GHz, 2 gigs) to 10.6, she can no longer drag and drop files onto certain applications in her dock to open the file. She has to go into the application and go to "open file" or whatever.
    For example, if she has a Word doc on her desktop, she cannot drag it onto the Word icon in her dock.
    Any advice?

    You have many issues with your and your wife's machine(s). Everything you've posted works here. Reinstall Snow Leopard, repair permissions, and see if that fixes things.

  • Can't drag and drop files in iTunes 7.6

    I've suddenly lost the ability to drag and drop files in iTunes, so I can't change the order of items within a playlist, move tracks from one playlist to another, or move a selection of tracks to an external drive. When I click & hold on a track, nothing happens at all; if you try to drag the file it doesn't move then bounce back - just no reaction at all. One day it worked fine, next time I launched iTunes the problem appeared. I'd had 7.6 up and running for about a month without this problem, and had no other software downloads or updates immediately before the problem appeared. I'd not changed any iTunes preferences either.
    I've checked that lefthand column in the playlist is selected, & that shuffle is turned off, & I'm not trying to move things into a smart list. I've tried trashing the com.apple plist, and repairing disk permissions. My iTunes library lives on an external hard drive, but this hasn't been an issue before. Drag and drop still works fine in other apps, so I don't suppose it's a mouse or Finder issue.
    I see a few other people have posted about this, but I haven't seen any solutions yet that worked for me. Am I missing something blindingly obvious? If anyone's got any ideas on this one I'd be eternally grateful - the ability to move things around easily was one of the main attractions of iTunes for me.

    Thanks David - that wasn't it, but I have managed to find a fix after trawling through various forums - I logged out then logged back in. Simple as that! Sadly I'm still none the wiser as to what caused the problem in the first place, so don't know if or when it'll reoccur. I also found it was affecting iPhoto after I first posted, so not an iTunes specific problem obviously. Thanks to all those who posted fixes in various forums, though some looked a little too scary for me to try out...

  • Can't drag and drop files into Indesign.

    My ability to dragon and drop files into InDesign has stopped. I also cannot copy and paste. I have tried dropping previously used jpg's and PDFs as well as copying text from Acrobat, Chrome and Word. I reset the prefrences with control option command shift, but still no luck - I can't drag and drop files. Also, I cannot move files with my mouse in the program.

    Nobody can help you without system information. Could be anything from graphics driver issues to a defective mouse to a busted operating system...
    Mylenium

  • I can't drag and drop files to playlist or to Shuffle.

    I have iTunes 10.5, and I can't drag and drop files to playlists or to my shuffle.  For playlists, I have to right click and "add to playlist" and for shuffle, I have to copy song and paste it into shuffle.  Is anyone else having this problem? How can I fix it?

    A lot of people are having this problem.  Most commonly, they are using a Wacom tablet, and sometimes updating its driver helps.  I don't have a Wacom, but still have the "can't drag" problem.  Here are the workarounds I have found in the forums so far...
    1. "gusto drag" -- slide the pointer across a song and click.
    2. clicking with both left and right mouse buttons.
    3. selecting items and do a "create playlist from selected". Then select the iPod and autofill from that playlist.

  • Still loose ability to drag and drop files in Project pane.

    In the last two weeks, I've had two instances where I lost
    the ability to drag and drop files within the Project pane.
    However, here's the kicker...the problem doesn't manefest itself
    until after I end my session, close RH, then restart the computer
    and restart the project. That means I don't know that I'm working
    in a broken project until AFTER I've zipped up and saved the
    current project. I tried addressing this in this orum once already
    and I received some suggested fixes (included in the list below)
    but it never fixed and and has happened again.
    Some pieces of information:
    - I've deleted the .xpj and cpd files, no joy.
    - I've regenerated the output, then tried to move a file, no
    joy.
    - I've generated a CHM (supposedy to update other files), no
    joy.
    - I'm not attempting to move a file to a directory where that
    name already exists
    - If while I'm having the problem, I create a nw project, new
    folders, and new topics, they move around fine.
    - If I create a topic IN the directory with the problem, I
    cannot move that file.
    - I've zippd up a broken project and sent it to Adobe. They
    openned it and it was fine. They zipped it back to me and it still
    wasn't.
    - If I create a new project, replicate my directory
    structure, and import the htm files into the new structure (a
    ubdirectory at a time), the files move fine.
    - I'm only working local on Windows 2000 with 30 GB of disk
    space, and I have not installed anything else on the computer in
    the last 2 weeks.
    - I've escalated this to the next level at Adobe and am
    sitting by my phone with my headset waiting for the call back
    "within the next two hours".
    Anyone seen this?

    quote:
    Originally posted by:
    Amebr
    Could the files have somehow been set to read-only?
    I'm not sure on the exact details of your process, but if you
    open the project after unzipping it, perhaps the unzip process has
    changed that file attribute...?
    I appreciate your response, but no...all files and
    directories had all permissions allowed. The zip process wasn't
    involved until aftrer the problem showed itself...I zip just for
    backup with the hope that I never have to go to it. It was my
    working project that lost the ability to move files. It just turns
    out that after it happened and I went to the zip that it showed
    that the problem had affected the files before I zipped them and
    therefore, the zip was of the broken set.

  • How to drag and drop files in Time Machine Capsule?

    Hi, all
    I have recently bought a Time Machine 2 TB. I have backed up my MacBook Air over the Time Machine. I was trying to copy or drag and drop files in the time capsule, but it wont allow me. It Neither does allow me to create any new folders in the Airport Time Machine. If i try to drag and drop files in it, it says, click to Authenticate, however, there is no place or tab to authenticate. I am the administrator.
    Please advise.
    Thanks

    It sounds like you are trying to copy / paste files to the Time Capsule Finder icon, when you should be copying / pasting to the drive on the Time Capsule, which is named "Data" unless you have changed it.
    Open any Finder window and look for the Time Capsule icon under the Shared heading on the left side of the window
    Click on the Time Capsule, which will display a folder to the right named Data, unless you have changed the name of the drive
    Double-cllick on Data to mount the drive on the desktop
    You can drag / drop or copy / paste files onto the drive icon, or open the drive and drag files into the open window.
    The first time that you do this, you may be asked for the Time Capsule device or disk password.

  • Drag and drop file into Outlook from AIR application

    I am hoping to see if this would be possible with the Adobe AIR API. We currently have a desktop AIR application that clients install in order to facilitate certain operations triggered via a web application (such as opening files directly from the web).
    One of the actions we are requested to support is the ability to drag and drop files from our web application directly into Outlook and have the file added as an attachment. I saw the documentation at Adobe Flash Platform * Dropping file promises and was wondering if this might be a possibility, or if there was some other method of performing this.
    We are trying to get around the fact that users now have to download a document to their desktop before being able to attach in Outlook.
    Thanks for any assistance.

    You just aren't calling the right NativeDragManager functions. See http://help.adobe.com/en_US/as3/dev/WS5b3ccc516d4fbf351e63e3d118666ade46-7d83.html

  • Drag and Drop files from other places to universal apps(windows store)

    Hi,
    Is there a possiblity to drag and drop a file from windows desktop or from other windows store app to my application. I tried searching samples, but it says about dragging an image with in the solution.
    The best example is drag and drop files into recycle bin. Is there a way possible for modern app(instead of recycle bin), or give an user similar experince as the same.
    If not windows8/windows8.1,could we expect in windows 10.
    I believe if it is not possible with XAML/c#, the same holds good for HTML5/JS
    Thanks & Regards Tejaswi Chandrapatla

    No. This is not supported.
    We cannot comment on upcoming versions.

Maybe you are looking for

  • How do i transfer information from one mac to another

    i have tried using migration assistant but dont want to tranfer all my files is there another way ?

  • Email Links In Smartforms emailed as a PDF

    All, I have written a program that creates a smartform as a PDF attachment which gets sent as an email. Part of the content contains URL's for the recipient to click and navigate to an page on our erecruitment system. The problem is that the url is o

  • Http submit button not working in acrobat pro

    I wanted submit data to website through http submit button. This button is only working PDF preview in LiveCycle Designer. However, when I open in Acrobat X pro, it shows error "Invalid server response". Would you tell me where is the problem? This b

  • Urgent help, Tree Viewer JSP

    Has anyone gotten the tree viewer bean to work on a JSP? If so, please give tips on it, or an example of table would be appreciated. I keep getting an error message that my row-set browser is not self-refferencial. Thanks

  • FMLE 3.2 capturing fewer frames after 8 hours of recording but no 'dropped frames' reported

    A client of ours is capturing 20x live streams via FMLECMD directly to F4V files (15min chunks) for ~21 hours a day. They're finding that after approx 8 hours of recording, FMLE gradually reduces the number of frames it's recording & the resulting fi