1

I'm new in using the DnD in java.I'm trying to drag an drop an image from a label to another. The first label is the source, the second is the destination. My trouble is that I need to drag the image from the source and recognize that i'm dropping on the correct destination; if the destination is correct the image from the source must disappear, else must come back to the source and notify it to the user using a window message or just a System.out.println(). I've tried using TransferHandler, DragSource, but I didn't get a single good result.

How to drag and drop an image from label to label?

The Drag Listener

public class DragMouseAdapter extends MouseAdapter {
        public void mousePressed(MouseEvent e) {
            JComponent c = (JComponent) e.getSource();
            TransferHandler handler = c.getTransferHandler();
            handler.exportAsDrag(c, e, TransferHandler.COPY);


        }
    }

The Source labels that contain the images

public ShipsGUI() {
    // setBorder(new EmptyBorder(10,10,10,10));
    setLayout(new GridLayout(2, 5));
    MouseListener listener = new DragMouseAdapter();
    for (int i = 0; i < 10; i++) {
        JPanel p = new JPanel(new BorderLayout(5, 0));
        JLabel a = new JLabel(ship,JLabel.CENTER);
        a.setName("ship");
        JLabel n = new JLabel("[" + Integer.toString(i + 1) + "]");
        n.setForeground(Color.BLUE);
        // a.setBorderPainted(false);
        // a.setBackground(Color.white);
        // a.setOpaque(true);
        //a.setIcon(ship,JLabel.CENTER);
        a.setTransferHandler(new TransferHandler("icon"));
        a.addMouseListener(listener);

        p.add(a);
        p.add(n, BorderLayout.LINE_START);
        add(p);

    }
}

The destination (it's a grid fo labels)

public NewAreaGioco(int r,int c, boolean enable){


    this.setLayout(new GridLayout(r,c,1,1));

    for(int i=0;i<r;i++){           
        for(int j=0;j<c;j++){
            JLabel l= new JLabel(" ");          
            l.setSize(30, 30);
            l.setBorder(BorderFactory.createLineBorder(Color.BLUE));
            if(enable)l.setTransferHandler(new TransferHandler("icon"));                
            add(l);
        }//fine for
    }//fine for
}
Luke Smith
  • 83
  • 1
  • 1
  • 9
  • 1
    Please post your code as [SSCCE](http://sscce.org), so we can see what's wrong. – Sergiy Medvynskyy Apr 06 '16 at 08:43
  • [Here](http://www.java2s.com/Tutorial/Java/0240__Swing/DragandDropSupportforImages.htm) is a DnD example for images. – Sergiy Medvynskyy Apr 06 '16 at 08:47
  • 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). – Andrew Thompson Apr 06 '16 at 08:49
  • 1
    Transfer the data, not the component for [example](http://stackoverflow.com/questions/28844574/drag-and-drop-from-jbutton-to-jcomponent-in-java/28844969#28844969) – MadProgrammer Apr 06 '16 at 09:10

1 Answers1

2

Well, you can't use the default TransferHandler class. You need to make your own.

I would start by looking at the DropDemo and ListTransferHandler class found on the examples page of the Drag and Drop tutorial.

A couple of change that I think you will need to make:

  1. export an image instead of text. I think the link provided by Sergiy above might help.

  2. The key point is in the exportDone(...) method. Your cleanup code would set the icon of the source component to null.

You will probably need to read the tutorial to understand the concept of these two classes.

camickr
  • 321,443
  • 19
  • 166
  • 288