0

The image you see below is a BufferedImage representation of a PDF. The bufferedimage is in an imageicon that is contained in a jlabel.

PDF File inside application window

What I am attempting to do here is to be able to drag that image out to another application in my system. If I drag any document (including images) into that application it accepts it. I need to drag drop this into that application as a PDF. I am using PDFBOX to help manipulate pdfs. My end users HAVE TO HAVE this implemented as drag drop. The other application that will be accepting these documents is written in a way so that once the document is in the system it cannot be removed and so drag drop will be the best way for them to do so.

Because, however, the image is contained in a jlabel I cannot drag it out, or at least I cannot find a way to do so. I can drag into a jlabel, but dragging out does not appear to be an option.

This is my first attempt at doing a drag drop built into an application of mine and I can only see the ability to drag a file url onto the desktop. Is there some way for me to be able to drag this (url or otherwise) out of the window at least onto the desktop? I am thinking that if I can get it onto the desktop I can get it into the other application.

I can save the file temporarily as a PDF which I believe I am going to have to do, but I just don't know how to get this file out of the JLabel. Any help is greatly appreciated.

Rabbit Guy
  • 1,840
  • 3
  • 18
  • 28
  • 1
    When I'm faced with something like, I start with something like [this](http://stackoverflow.com/questions/13597233/how-to-drag-and-drop-files-from-a-directory-in-java/13597635#13597635) or [this](http://stackoverflow.com/questions/15080244/java-drag-n-drop-files-of-specific-extension-on-jframe/15080654#15080654). The reason for this, is I create a test app which allows you to drag a file in from the file system and dump the available DataFlavors, this lets you know what the other application is likely to see. – MadProgrammer Oct 02 '15 at 21:21
  • 1
    From this you can determine (or play with) the DataFlavors that the other app might use. From this, you could generate the required export options directly from your app – MadProgrammer Oct 02 '15 at 21:21
  • 1
    Although your question isn't about PDFBox despite being tagged, coincidentally, PDFDebugger (the 2.0 version) has a drag and drop feature, so you may want to look at its source code. – Tilman Hausherr Oct 02 '15 at 22:04

1 Answers1

0

My design was wrong which was why DnD was not working. In the setup I had, the image above was contained in an ImageIcon which in itself was contained within a JLabel. The coding to drag the contents of a JLabel became the issue as JLabels are not setup to drag out of by default (or you cannot do it at all).

When I changed the setup so that my image was in an ImageIcon that was contained in a JList the setup became fairly strait forward. My other application would take any flavor thrown at it, but specifically I needed a PDF in it.

To do this, the JFrame that contained the Jlist had to implement the DragDropListener (obviously). The magic code was in the FileTransferHandler class:

private class FileTransferHandler extends TransferHandler {
    @Override
    protected Transferable createTransferable(JComponent c) {
        List<File> files = new ArrayList<>();
        files.add(currentPDF.getFile());
        return new FileTransferable(files);
    }
    @Override
    public int getSourceActions(JComponent c) {
        return MOVE;
    }        
}

-

private class FileTransferable implements Transferable {

    private final List<File> files;

    public FileTransferable(List<File> files) {
        this.files = files;
    }
    @Override
    public DataFlavor[] getTransferDataFlavors() {
        return new DataFlavor[]{DataFlavor.javaFileListFlavor};
    }
    @Override
    public boolean isDataFlavorSupported(DataFlavor flavor) {
        return flavor.equals(DataFlavor.javaFileListFlavor);
    }
    @Override
    public Object getTransferData(DataFlavor flavor)
            throws UnsupportedFlavorException, IOException {
        if (!isDataFlavorSupported(flavor)) {
            throw new UnsupportedFlavorException(flavor);
        }
        return files;
    }
}   

currentPDF.getFile() is a method written into my PDF object that would return a finalized file of what would be dragged into the other application. I left the option to transfer multiple files at once should it be needed in the future hence the ArrayLists.

Rabbit Guy
  • 1,840
  • 3
  • 18
  • 28