Drag and Drop- showing image when dragging

Posted on 2001-07-31
Last Modified: 2006-11-17
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!!

Question by:j_baxter99
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
LVL 92

Expert Comment

ID: 6337114
If your target environment is Windows then unfortunately I don't think you can.
LVL 92

Expert Comment

ID: 6337130
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.

LVL 15

Expert Comment

ID: 6337333
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 15

Expert Comment

ID: 6337336
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.
LVL 15

Accepted Solution

ozymandias earned 75 total points
ID: 6337346
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.

Author Comment

ID: 6339514
pretty **** that you cant do it on windows!!!

cheers lads!
LVL 15

Expert Comment

ID: 6339567
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.
LVL 92

Expert Comment

ID: 6339589
Cursors work, drag images don't.

Author Comment

ID: 6343728
sorry , i meant the images on windows!(not cursor)

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question