2 Replies - 4048 Views - Last Post: 27 December 2009 - 07:33 PM Rate Topic: -----

#1 painkiller102   User is offline

  • D.I.C Regular
  • member icon

Reputation: 18
  • View blog
  • Posts: 281
  • Joined: 27-February 08

Taking a file from the System Clipboard

Post icon  Posted 27 December 2009 - 02:13 PM

I am building a desktop application and i am now adding features to it. I know that you can retrieve images and strings from the system clipboard, however what about files? Specifically what i am trying to create is the following scenario:

User Right clicks on a folder
User Selects "Copy"
Program retrieves the folder that was copied.


Now as far as code, i don't have much in this section. My previous thoughts however was to try something like this:

	public void clipboardTest()
	{
		Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
		Transferable tran = clipboard.getContents(null);
		try
		{
			if(tran != null && tran.isDataFlavorSupported(DataFlavor.javaFileListFlavor))
			{
				File[] list = (File[]) tran.getTransferData(DataFlavor.javaFileListFlavor);
			}
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
	}



but it clearly didn't work :) .
Is there anyway to accomplish this task? (well clearly there is if you can get the images off of the system clipboard). Or does XP, Vista, and Win7 put that file elsewhere other than the System Clipboard? Any Help is greatly appreciated!

Is This A Good Question/Topic? 0
  • +

Replies To: Taking a file from the System Clipboard

#2 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Taking a file from the System Clipboard

Posted 27 December 2009 - 06:34 PM

Transferable.getTransferData(DataFlavor.javaFileListFlavor);

does not return an array of File[] as you coded it but an ArrayList of Object
This works for me

import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.*;
import java.awt.event.*;

public class TestClipboard extends JFrame implements ActionListener {

	TestClipboard() {
		super("Test clipboard");
		setSize(200, 80);
		JButton b = new JButton("Clip");
		b.addActionListener(this);
		add(b);
		setVisible(true);
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	}
	public void actionPerformed(ActionEvent e) {
		Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
		Transferable tran = clipboard.getContents(null);
		try
		{
			if(tran != null && tran.isDataFlavorSupported(DataFlavor.javaFileListFlavor))
			{
				Object o =  tran.getTransferData(DataFlavor.javaFileListFlavor);
				System.out.println(o);
		   }
		}
		catch(Exception ex)
		{
			System.out.println("Exception: " + ex);
		}
	}
	
	public static void main(String[] args) {
		new TestClipboard();
	}
}


Happy to see a justified "Intermediate" tag once on a while :)

This post has been edited by pbl: 27 December 2009 - 06:36 PM

Was This Post Helpful? 1
  • +
  • -

#3 painkiller102   User is offline

  • D.I.C Regular
  • member icon

Reputation: 18
  • View blog
  • Posts: 281
  • Joined: 27-February 08

Re: Taking a file from the System Clipboard

Posted 27 December 2009 - 07:33 PM

Nice. It works well for me too now! Thanks for the post! I can finally finish my paste function now!!!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1