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
andDragSourceListener
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:
001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093 094 095 096 097 098 099 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 | 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.