dnd

Drag and Drop example

In this tutorial we are going to see how to build a simple application that performs drag and drop operations in a Java Desktop Application. As you know the drag and drop operation is very important in an Application that has to deal with graphical objects.

In short, in order to add drag and drop functionality to your application you have to:

  • Create a class that implements DragGestureListener and DragSourceListener interfaces. Use this when you want to make an object draggable.
  • Override dragGestureRecognized, dragEnter called when the user is dragging this drag source and enters the drop target, dragOver called when the user is dragging this drag source and moves over the drop target, dragExit called when the user is dragging this drag source and leaves the drop target, dropActionChanged called when the user changes the drag action between copy or move, dragDropEnd called when the user finishes or cancels the drag operation.
  • Create a class that implements DropTargetListener. Use this to make a component a drop target.

Let’s see the code:

package com.javacodegeeks.snippets.desktop;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
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.IOException;

public class DragAndDropExample {

  public static void main(String[] args) {

    // Create a frame
    Frame frame = new Frame("Example Frame");

    /*

* Create a container with a flow layout, which arranges its children

* horizontally and center aligned. A container can also be created with

* a specific layout using Panel(LayoutManager) constructor, e.g.

* Panel(new FlowLayout(FlowLayout.RIGHT)) for right alignment

*/
    Panel panel = new Panel();

    // Add a drop target text area in the center of the frame
    Component textArea = new DropTargetTextArea();
    frame.add(textArea, BorderLayout.CENTER);

    // Add several draggable labels to the container
    Label helloLabel = new DraggableLabel("Hello");
    Label worldLabel = new DraggableLabel("World");
    panel.add(helloLabel);
    panel.add(worldLabel);

    // Add the container to the bottom of the frame
    frame.add(panel, BorderLayout.SOUTH);

    // Display the frame
    int frameWidth = 300;
    int frameHeight = 300;
    frame.setSize(frameWidth, frameHeight);

    frame.setVisible(true);

  }

  // Make a Label draggable; You can use the example to make any component draggable
  public static class DraggableLabel extends Label implements DragGestureListener, DragSourceListener {
    DragSource dragSource;

    public DraggableLabel(String text) {

setText(text);

dragSource = new DragSource();

dragSource.createDefaultDragGestureRecognizer(this, DnDConstants.ACTION_COPY_OR_MOVE, this);
    }

    public void dragGestureRecognized(DragGestureEvent evt) {

Transferable transferable = new StringSelection(getText());

dragSource.startDrag(evt, DragSource.DefaultCopyDrop, transferable, this);
    }

    public void dragEnter(DragSourceDragEvent evt) {

// Called when the user is dragging this drag source and enters the drop target

System.out.println("Drag enter");
    }

    public void dragOver(DragSourceDragEvent evt) {

// Called when the user is dragging this drag source and moves over the drop target

System.out.println("Drag over");
    }

    public void dragExit(DragSourceEvent evt) {

// Called when the user is dragging this drag source and leaves the drop target

System.out.println("Drag exit");
    }

    public void dropActionChanged(DragSourceDragEvent evt) {

// Called when the user changes the drag action between copy or move

System.out.println("Drag action changed");
    }

    public void dragDropEnd(DragSourceDropEvent evt) {

// Called when the user finishes or cancels the drag operation

System.out.println("Drag action End");
    }

  }

  // Make a TextArea a drop target; You can use the example to make any component a drop target
  public static class DropTargetTextArea extends TextArea implements DropTargetListener {

    public DropTargetTextArea() {

new DropTarget(this, this);
    }

    public void dragEnter(DropTargetDragEvent evt) {

// Called when the user is dragging and enters this drop target

System.out.println("Drop enter");
    }

    public void dragOver(DropTargetDragEvent evt) {

// Called when the user is dragging and moves over this drop target

System.out.println("Drop over");
    }

    public void dragExit(DropTargetEvent evt) {

// Called when the user is dragging and leaves this drop target

System.out.println("Drop exit");
    }

    public void dropActionChanged(DropTargetDragEvent evt) {

// Called when the user changes the drag action between copy or move

System.out.println("Drop action changed");
    }

    public void drop(DropTargetDropEvent evt) {

// Called when the user finishes or cancels the drag operation

try {

    Transferable transferable = evt.getTransferable();

    if (transferable.isDataFlavorSupported(DataFlavor.stringFlavor)) {

  evt.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);

  String dragContents = (String) transferable.getTransferData(DataFlavor.stringFlavor);

  evt.getDropTargetContext().dropComplete(true);

  // We append the label text to the text area when dropped

  setText(getText() + " " + dragContents);

    } else {

  evt.rejectDrop();

    }

} catch (IOException e) {

    evt.rejectDrop();

} catch (UnsupportedFlavorException e) {

    evt.rejectDrop();

}
    }

  }

}

 
This was an example on how to create an application that performs drag and drops operations.

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button