Home Java Example Java Swing Drag and Drop components from one frame to another frame
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Drag and Drop components from one frame to another frame

Advertisement
Java code given below shows that how to drag and drop component (drop down list, text area, check box, radio button etc.) from one frame to another frame.

Drag and Drop components from one frame to another frame

     

Java code given below shows that how to drag and drop component (drop down list, text area, check box, radio button etc.) from one frame to another frame. In this code we have used following interfaces.....

1: DragGestureListener : This interface is invoked when an object of that class or its subclass detects a drag initiating gesture.

2: DragSourceListener : This interface provides event notification to the application for DragSource events.

3: DropTargetListener : This interface is used by the DropTarget class to provide notification of DnD operations.

4: Transferable : This interface provides data for a transfer operation.

DragDrop.java

import java.awt.*;
import java.awt.datatransfer.*;
import java.awt.dnd.*;
public class DragDrop implements DragGestureListener, 
DragSourceListener,
        DropTargetListener, Transferable {
    static final DataFlavor[] supportedFlavors = {null};
    
    static {
        try {
            supportedFlavors[0] = new 
            DataFlavor(DataFlavor.javaJVMLocalObjectMimeType);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    Object object;
    // Transferable methods.
    public Object getTransferData(DataFlavor flavor) {
        if (flavor.isMimeTypeEqual
            (DataFlavor.javaJVMLocalObjectMimeType)) {
            return object;
        } else {
            return null;
        }
    }
    public DataFlavor[] getTransferDataFlavors() {
        return supportedFlavors;
    }
    public boolean isDataFlavorSupported(DataFlavor flavor) {
        return flavor.isMimeTypeEqual
        (DataFlavor.javaJVMLocalObjectMimeType);
    }
    // DragGestureListener method.
    public void dragGestureRecognized(DragGestureEvent ev) {
        ev.startDrag(null, this, this);
    }
    // DragSourceListener methods.
    public void dragDropEnd(DragSourceDropEvent ev) {
    }
    public void dragEnter(DragSourceDragEvent ev) {
    }
    public void dragExit(DragSourceEvent ev) {
    }
    public void dragOver(DragSourceDragEvent ev) {
        object = ev.getSource();
    }
    public void dropActionChanged(DragSourceDragEvent ev) {
    }
    // DropTargetListener methods.
    public void dragEnter(DropTargetDragEvent ev) {
    }
    public void dragExit(DropTargetEvent ev) {
    }
    public void dragOver(DropTargetDragEvent ev) {
        dropTargetDrag(ev);
    }
    public void dropActionChanged(DropTargetDragEvent ev) {
        dropTargetDrag(ev);
    }
    void dropTargetDrag(DropTargetDragEvent ev) {
        ev.acceptDrag(ev.getDropAction());
    }
    public void drop(DropTargetDropEvent ev) {
        ev.acceptDrop(ev.getDropAction());
        try {
            Object target = ev.getSource();
            Object source = ev.getTransferable().getTransferData
                            (supportedFlavors[0]);
            Component component = ((DragSourceContext) 
                                    source).getComponent();
            Container oldContainer = component.getParent();
            Container container = (Container) ((DropTarget) 
                                   target).getComponent();
            container.add(component);
            oldContainer.validate();
            oldContainer.repaint();
            container.validate();
            container.repaint();
        } 
        catch (Exception ex) {
            ex.printStackTrace();
        }
        ev.dropComplete(true);
    }
    public static void main(String[] arg) {
        Button button = new Button("Drag this button");
        Label label = new Label("Drag this label");
        Checkbox checkbox = new Checkbox("Drag this check box");
        CheckboxGroup radiobutton = new CheckboxGroup();
        Checkbox checkbox1 = new Checkbox("Drag this check box", 
        radiobutton, false);
        Choice country = new Choice();
        // adding possible choices
        country.add("India");
        country.add("US");
        country.add("Australia");
        Frame source = new Frame("Source Frame");
        source.setLayout(new FlowLayout());
        source.add(button);
        source.add(label);
        source.add(checkbox);
        source.add(checkbox1);
        source.add(country);
        Frame target = new Frame("Target Frame");
        target.setLayout(new FlowLayout());
        DragDrop dndListener = new DragDrop();
        DragSource dragSource = new DragSource();
        DropTarget dropTarget1 = new DropTarget(source, 
        DnDConstants.ACTION_MOVE, dndListener);
        DropTarget dropTarget2 = new DropTarget(target, 
        DnDConstants.ACTION_MOVE, 
                dndListener);
        DragGestureRecognizer dragRecognizer1 = dragSource.
                createDefaultDragGestureRecognizer(button, 
                DnDConstants.ACTION_MOVE, dndListener);
        DragGestureRecognizer dragRecognizer2 = dragSource.
                createDefaultDragGestureRecognizer(label, 
                DnDConstants.ACTION_MOVE, dndListener);
        DragGestureRecognizer dragRecognizer3 = dragSource.
                createDefaultDragGestureRecognizer(checkbox, 
                DnDConstants.ACTION_MOVE, dndListener);
        DragGestureRecognizer dragRecognizer4 = dragSource.
                createDefaultDragGestureRecognizer(checkbox1, 
                DnDConstants.ACTION_MOVE, dndListener);
        DragGestureRecognizer dragRecognizer5 = dragSource.
                createDefaultDragGestureRecognizer(country, 
                DnDConstants.ACTION_MOVE, dndListener);
        source.setBounds(0, 200, 200, 200);
        target.setBounds(220, 200, 200, 200);
        source.show();
        target.show();
    }
} 

Compile this java code with javac command  from command prompt then run. When run this code this shows two separate frames.......

 One frame has five component, user can drag and drop component from one to another frame.

Download Source Code

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: July 29, 2008

Ask Questions?    Discuss: Drag and Drop components from one frame to another frame   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
Vee
March 21, 2012
Not Enough Explanation -Drag and Drop Componenets

Hai! I am just wondering ,why there is no enough explanations about the Drag and Drop classes and interfaces are given.If you could give some more detailed explanation ,that would definitely give a better understanding of what these APIs are doing. Thanks!Otherwise Its a brilliant tutorial.reply would be much appreciated,.
DMCA.com