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.

Drag and Drop components 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

Tutorials

  1. Display Image in Java
  2. Show Coordinates
  3. What is Java Swing?
  4. Creating a Frame
  5. Setting an Icon for a Frame in Java
  6. Show Dialog Box in Java
  7. Show message and confirm dialog box
  8. Show input dialog box
  9. Changing the Label of a JButton Component in Java
  10. Setting Multi-Line label on the Button
  11. Setting icon on the button in Java
  12. Making a Frame Non Resizable in Java
  13. Remove Minimize and Maximize Button of Frame in Java
  14. Removing the Title Bar of a Frame
  15. Setting Bounds for a maximized frame
  16. Iconifying and Maximizing a frame in Java
  17. Making a component drag gable in java
  18. Create a ToolBar in Java
  19. Animating Images in Java Application
  20. Drawing with Color in Java
  21. Drawing with Gradient Color in Java
  22. Adding a Rollover and Pressed Icon to a JButton Component in Java
  23. Creating Check Box in Java Swing
  24. Customize the Icon in a JCheckBox Component of Java Swing
  25. Create a JComboBox Component in Java
  26. Combo Box operation in Java Swing
  27. Create a JRadioButton Component in Java
  28. Selecting a Radio Button component in Java
  29. Create a JList Component in Java
  30. Setting Tool Tip Text for items in a JList Component
  31. Setting the Dimensions of an Item in a JList Component in Java
  32. Create a JSpinner Component in Java
  33. Show Time in JSpinner Component
  34. Disabling Keyboard Editing in a JSpinner Component
  35. Limiting the Values in a Number JSpinner Component
  36. JSlider Component of Java Swing
  37. Progress Bar in Java Swing
  38. Create menus and submenus in Java
  39. Crate a Popup Menu in Java
  40. Create a Popup Menus with Nested Menus in Java