Java: How to drag a file from a JTable to native filesystem?

My Java 7 Swing application features a JTable that contains the following objects:

public class MyFile {

    private long id;
    private long created;
    private long modified;
    private String description;
    private File file;

    public MyFile(long id) {
        this.id = id;
        this.created = System.currentTimeMillis();
    }

    // getter & setter methods ...

}

The goal is to drag these MyFile objects out of my application and drop them to the filesystem (e.g. to the desktop). When setting setDragEnabled(true) to my JTable, the icon already turns to a "+" symbol when dragging a table entry, as known by regular drag&drop actions from other applications. But when actually dropping the object to the desktop nothing happens...

How can I tell my JTable to only drop the File object inside the MyFile object ? Did I forgot more things ?

Could someone provide a short sample ?

Many thanks for your help in advance!

Answers


You must make sure that the created Transferable contains the DataFlavor#javaFileListFlavor and that the data for that flavor is the File contained in your MyFile instance (to be more precise: a List with the File, as explained in the javadoc of that flavor).

This will probably require a custom TransferHandler on your JTable.


Need Your Help

Implementing fragments in Android

android fragment

I am developing a application and in an activity I want it to be similar to the ICS settings application; i.e. on the left side I have a ListFragment with 4 categories, and on the right a Fragment ...

how to put a custom view inside of a custom viewGroup / Layout

android user-interface android-custom-view

custom view inside of a custom viewGroup not visible, how can I get it to show up?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.