Drag and Drop to Windows Explorer

Draging a JLabel from a Panel to Explorer

Page 1 of 1

10 Replies - 9308 Views - Last Post: 18 January 2010 - 01:43 AM Rate Topic: -----

#1 painkiller102   User is offline

  • D.I.C Regular
  • member icon

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

Drag and Drop to Windows Explorer

Post icon  Posted 08 January 2010 - 01:59 PM

I have been playing around with this problem for some time now and have come up with nothing. I have googled it as well and still come up with a blank. I would like to be able to drag a JLabel from my JPanel to windows explorer and have it move the file. Like below:

JLabel called "C:\Test\Image.png"
I click and drag the JLabel to "C:\FinalLocation\Done\"
now the program will move the file to the destination.

The only thing i have currently found is support for dragging Stuff from windows explorer to a JPanel or JLabel, like the code below:
package draganddroptesting;

import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DragGestureEvent;
import java.awt.dnd.DragGestureListener;
import java.awt.dnd.DragSource;
import java.awt.dnd.DragSourceDragEvent;
import java.awt.dnd.DragSourceDropEvent;
import java.awt.dnd.DragSourceEvent;
import java.awt.dnd.DragSourceListener;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetEvent;
import java.awt.dnd.DropTargetListener;
import java.io.File;
import javax.swing.DefaultListModel;
import javax.swing.JLabel;

public class dndtest extends JLabel implements DropTargetListener, DragSourceListener, DragGestureListener
{
	DropTarget dropTarget = new DropTarget (this, this);
	DragSource dragSource = DragSource.getDefaultDragSource();

	public dndtest()
	{
		this.setText("Test Drag");
		dragSource.createDefaultDragGestureRecognizer(this, DnDConstants.ACTION_COPY_OR_MOVE, this);
	}

	public void dragEnter(DropTargetDragEvent dtde) 
	{
		dtde.acceptDrag (DnDConstants.ACTION_COPY_OR_MOVE);
	}

	public void dragOver(DropTargetDragEvent dtde) {
	}

	public void dropActionchanged(DropTargetDragEvent dtde) {
	}

	public void dragExit(DropTargetEvent dte) {
	}

	public synchronized void drop(DropTargetDropEvent dtde)
	{
		try
		{
			Transferable tr = dtde.getTransferable();
			if (tr.isDataFlavorSupported (DataFlavor.javaFileListFlavor))
			{
				dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
				Object o =  tr.getTransferData(DataFlavor.javaFileListFlavor);
				char[] array = o.toString().toCharArray();
				String path = "";
				for(int i = 0; i < array.length - 1;i++)
				{
					path = path + array[i];
				}
				File file = new File(path);
				System.out.println(file.getAbsolutePath());
				dtde.getDropTargetContext().dropComplete(true);
			}
			else
			{
				System.out.print("Rejected");
				dtde.rejectDrop();
			}
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
	}

	public void dragEnter(DragSourceDragEvent dsde) {
	}

	public void dragOver(DragSourceDragEvent dsde) {
	}

	public void dropActionchanged(DragSourceDragEvent dsde) {
	}

	public void dragExit(DragSourceEvent dse) {
	}

	public void dragDropEnd(DragSourceDropEvent dsde) {
	}

	public void dragGestureRecognized(DragGestureEvent dge) 
	{
		
	}
}



I need to do the EXACT OPPOSITE of the above ... unfortunately! I want to drag from my Panel to windows explorer!!! Any help is greatly appreciated and thank in advanced for any help given!

Is This A Good Question/Topic? 0
  • +

Replies To: Drag and Drop to Windows Explorer

#2 erik.price   User is offline

  • D.I.C Lover
  • member icon

Reputation: 486
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Drag and Drop to Windows Explorer

Posted 08 January 2010 - 02:07 PM

I have never used the API, but the demo program on the tutorial of Drag and Drop from Sun's website worked fine for me (dragging and dropping both ways)

Here's the source if you want to check it out. http://java.sun.com/...d/BasicDnD.java

And here's the tutorial
http://java.sun.com/.../basicdemo.html

Hopefully someone else will be able to be more helpful :P
Was This Post Helpful? 0
  • +
  • -

#3 painkiller102   User is offline

  • D.I.C Regular
  • member icon

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

Re: Drag and Drop to Windows Explorer

Posted 08 January 2010 - 02:12 PM

View Posterik.price, on 8 Jan, 2010 - 01:07 PM, said:

I have never used the API, but the demo program on the tutorial of Drag and Drop from Sun's website worked fine for me (dragging and dropping both ways)

Here's the source if you want to check it out. http://java.sun.com/...d/BasicDnD.java

And here's the tutorial
http://java.sun.com/.../basicdemo.html

Hopefully someone else will be able to be more helpful :P


That drag and drop allows it to drag strings to other components inside of java. It doesn't support me dragging something from my Panel to Windows Explorer.
Was This Post Helpful? 0
  • +
  • -

#4 erik.price   User is offline

  • D.I.C Lover
  • member icon

Reputation: 486
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Drag and Drop to Windows Explorer

Posted 08 January 2010 - 02:17 PM

Did you hit the tick mark in the bottom left corner that said "Turn on Drag and Drop"?

I'm not sure why it doesn't work for you; it works fine for me. (I drag the file from Java to my desktop or somewhere and it appears as a file, maybe we have different ideas about what 'working' is?)
Was This Post Helpful? 0
  • +
  • -

#5 painkiller102   User is offline

  • D.I.C Regular
  • member icon

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

Re: Drag and Drop to Windows Explorer

Posted 08 January 2010 - 02:54 PM

View Posterik.price, on 8 Jan, 2010 - 01:17 PM, said:

Did you hit the tick mark in the bottom left corner that said "Turn on Drag and Drop"?

I'm not sure why it doesn't work for you; it works fine for me. (I drag the file from Java to my desktop or somewhere and it appears as a file, maybe we have different ideas about what 'working' is?)



hmmm. Yes i did click the tick mark, and yea. it wont let me drag things the the desktop at all
Was This Post Helpful? 0
  • +
  • -

#6 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2972
  • View blog
  • Posts: 11,223
  • Joined: 15-July 08

Re: Drag and Drop to Windows Explorer

Posted 08 January 2010 - 05:52 PM

I see your favorite OS is Windows and Erik.Price uses Linux. Could THAT have any bearing on the situation?
Was This Post Helpful? 0
  • +
  • -

#7 erik.price   User is offline

  • D.I.C Lover
  • member icon

Reputation: 486
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Drag and Drop to Windows Explorer

Posted 08 January 2010 - 06:04 PM

@dogstopper I was actually using Mac OSX when I posted that, I didn't have access to my Windows/Linux computer.
Was This Post Helpful? 0
  • +
  • -

#8 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2972
  • View blog
  • Posts: 11,223
  • Joined: 15-July 08

Re: Drag and Drop to Windows Explorer

Posted 08 January 2010 - 06:13 PM

View Posterik.price, on 8 Jan, 2010 - 07:04 PM, said:

@dogstopper I was actually using Mac OSX when I posted that, I didn't have access to my Windows/Linux computer.


Alright, just a thought
Was This Post Helpful? 0
  • +
  • -

#9 erik.price   User is offline

  • D.I.C Lover
  • member icon

Reputation: 486
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Drag and Drop to Windows Explorer

Posted 08 January 2010 - 06:22 PM

Good thought, but it works on my Windows PC as well.

Don't worry so much about the demo, and instead look more at the source code
Was This Post Helpful? 0
  • +
  • -

#10 painkiller102   User is offline

  • D.I.C Regular
  • member icon

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

Re: Drag and Drop to Windows Explorer

Posted 10 January 2010 - 09:54 PM

ok, i've been over the code up and down, and there isn't support for the drag and drop To the desktop. Maybe my description isn't clear and were all getting confused oO. Ok. so the ultimate goal for me (in simple terms) is to be able to drag a JLabel from my JPanel to My desktop. like:
JLabel label = new JLabel("C:/Test/test.png");



The label basically represents a file, and when i drag it from the panel to the Desktop, it should be moved to the desktop. I already have support for Dragging items from the desktop to my JPanel, but not for dragging my JLabel to the desktop! again any help is greatly appreciated!
Was This Post Helpful? 0
  • +
  • -

#11 daser   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 18-January 10

Re: Drag and Drop to Windows Explorer

Posted 18 January 2010 - 01:43 AM

View Postpainkiller102, on 10 Jan, 2010 - 08:54 PM, said:

ok, i've been over the code up and down, and there isn't support for the drag and drop To the desktop. Maybe my description isn't clear and were all getting confused oO. Ok. so the ultimate goal for me (in simple terms) is to be able to drag a JLabel from my JPanel to My desktop. like:
JLabel label = new JLabel("C:/Test/test.png");



The label basically represents a file, and when i drag it from the panel to the Desktop, it should be moved to the desktop. I already have support for Dragging items from the desktop to my JPanel, but not for dragging my JLabel to the desktop! again any help is greatly appreciated!



Hi, I have same problem. Did you found solution yet? Could you please share it with us :)
Regards
DaSeR
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1