Drag and Drop- showing image when dragging

how do i show a custom image when i am dragging??

heres is a sample of my code
public class CurrentObjectsLabel extends JLabel
    implements DragSourceListener, DragGestureListener    {

  DragSource dragSource = null;
  java.awt.Image image2 = null;
  public CurrentObjectsLabel(String name,Icon image,int value) {
    dragSource = new DragSource();
    dragSource.createDefaultDragGestureRecognizer( this, DnDConstants.ACTION_MOVE, this);

  public void dragGestureRecognized( DragGestureEvent event) {
       String name = getText();
     System.out.println("Moving oject "+name);
     StringSelection text = new StringSelection(name);
     Point p = new Point(0,0);
        dragSource.startDrag (event,DragSource.DefaultMoveDrop,image2,p,text, this);

 i havent tried using the dragSource.createDefaultDragGestureRecongizer(java.awt.dnd.peer.DragSourceContextPeer dscp, DragGestureEvent dgl, Cursor dragCursor, Image dragImage, Point imageOffset, Transferable t, DragSourceListener dsl)

method because im new to the whole DnD package and wouldnt know where to start for creating a DragSourceContextPeer!!

if anyone can show me some code as to how its done it would be a great help!!
thanks in advance!!

Who is Participating?
ozymandiasConnect With a Mentor Commented:
Sorry I should have said you specify a cursor, not an icon.

In the startDrag method the DragSource.DefaultMoveDrop object is the cursor that will be used and it is the default drag cursor for the platform. You can replace it with any cursor of your choice.
If your target environment is Windows then unfortunately I don't think you can.
What your doing looks correct (specifying the image in startDrag) so I'm guessing your on a Windows platform.

The problem is apparently caused by MS.

7 new features that'll make your work life better

It’s our mission to create a product that solves the huge challenges you face at work every day. In case you missed it, here are 7 delightful things we've added recently to monday to make it even more awesome.

You just specify an Icon in the startDrag method.
Here is the code :

public void dragGestureRecognized(DragGestureEvent dge){
    ImageIcon icon = new ImageIcon("custom.gif");
    Toolkit tk = this.getToolkit();
    Cursor cursor = tk.createCustomCursor(icon.getImage(), new Point(0,0), "cursor");
    dragSource.startDrag(dge, cursor, ti, this);

You just need to specify the image you want to use in :

    ImageIcon icon = new ImageIcon("custom.gif");

instead of "custom.gif

NB : you can use any string instead of "cursor".
The Point is the hotspot of the cursor, i.e. the active bit of the cursor.
j_baxter99Author Commented:
pretty **** that you cant do it on windows!!!

cheers lads!
You can do it on windows.
The code I posted works fine on windows.
Or at least it works fine on my Windows machine which is NT4.
Cursors work, drag images don't.
j_baxter99Author Commented:
sorry , i meant the images on windows!(not cursor)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.