Create a drag source a drop target and a transferable object. : Drag Drop « Swing JFC « Java






Create a drag source a drop target and a transferable object.

  

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.datatransfer.DataFlavor;
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.DropTarget;
import java.awt.dnd.DropTargetAdapter;
import java.awt.dnd.DropTargetDropEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class ComplexExample extends JFrame implements DragGestureListener {
  public ComplexExample() {
    JPanel left = new JPanel();
    left.setBackground(Color.red);

    JPanel right = new JPanel();
    right.setBackground(Color.white);

    new MyDropTargetListener(right);

    DragSource ds = new DragSource();
    ds.createDefaultDragGestureRecognizer(left, DnDConstants.ACTION_COPY, this);

    setLayout(new FlowLayout());
    add(left);
    add(right);

    setSize(40,50);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocationRelativeTo(null);
    setVisible(true);
  }

  public void dragGestureRecognized(DragGestureEvent event) {
    Cursor cursor = null;
    JPanel panel = (JPanel) event.getComponent();

    Color color = panel.getBackground();
    if (event.getDragAction() == DnDConstants.ACTION_COPY) {
      cursor = DragSource.DefaultCopyDrop;
    }
    event.startDrag(cursor, new TransferableColor(color));
  }

  class MyDropTargetListener extends DropTargetAdapter {
    private DropTarget dropTarget;
    private JPanel panel;

    public MyDropTargetListener(JPanel panel) {
      this.panel = panel;
      dropTarget = new DropTarget(panel, DnDConstants.ACTION_COPY, this, true, null);
    }

    public void drop(DropTargetDropEvent event) {
      try {
        Transferable tr = event.getTransferable();
        Color color = (Color) tr.getTransferData(TransferableColor.colorFlavor);
        if (event.isDataFlavorSupported(TransferableColor.colorFlavor)) {
          event.acceptDrop(DnDConstants.ACTION_COPY);
          this.panel.setBackground(color);
          event.dropComplete(true);
          return;
        }
        event.rejectDrop();
      } catch (Exception e) {
        e.printStackTrace();
        event.rejectDrop();
      }
    }
  }

  public static void main(String[] args) {
    new ComplexExample();
  }
}

class TransferableColor implements Transferable {
  protected static DataFlavor colorFlavor = new DataFlavor(Color.class, "A Color Object");
  protected static DataFlavor[] supportedFlavors = { colorFlavor };
  Color color;
  public TransferableColor(Color color) {
    this.color = color;
  }

  public DataFlavor[] getTransferDataFlavors() {
    return supportedFlavors;
  }

  public boolean isDataFlavorSupported(DataFlavor flavor) {
    if (flavor.equals(colorFlavor) || flavor.equals(DataFlavor.stringFlavor))
      return true;
    return false;
  }

  public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException {
    if (flavor.equals(colorFlavor))
      return color;
    else if (flavor.equals(DataFlavor.stringFlavor))
      return color.toString();
    else
      throw new UnsupportedFlavorException(flavor);
  }
}

   
    
  








Related examples in the same category

1.Tree: Drag and DropTree: Drag and Drop
2.Drag and dropDrag and drop
3.Adding Image-Dragging Behavior
4.Dropper - show File Drop Target from Drag-n-DropDropper - show File Drop Target from Drag-n-Drop
5.Demonstrate various aspects of Swing data transferDemonstrate various aspects of Swing data transfer
6.File Tree Drop TargetFile Tree Drop Target
7.File Tree Drag SourceFile Tree Drag Source
8.Editor Drop Target 4Editor Drop Target 4
9.Drag Drop Tree ExampleDrag Drop Tree Example
10.JLabel Drag Source JLabel Drag Source
11.Panel Drop TargetPanel Drop Target
12.Editor Drop Target 3Editor Drop Target 3
13.Editor Drop TargetEditor Drop Target
14.Editor Drop Target 2Editor Drop Target 2
15.JTextArea subclass allows TransferableColor objects toJTextArea subclass allows TransferableColor objects to
16.Transferable Color
17.Color Drag Source
18.A sample component for dragging and dropping a collection of files into a tree.A sample component for dragging and dropping a collection of files into a tree.
19.Test of the DragGesture classes and JList to see if weTest of the DragGesture classes and JList to see if we
20.A TransferHandler and JTextArea that will accept any drop at allA TransferHandler and JTextArea that will accept any drop at all
21.Drag and drop: TextAreaDrag and drop: TextArea
22.Drag capabilities: JListDrag capabilities: JList
23.A simple drop tester application for JDK 1.4 Swing componentsA simple drop tester application for JDK 1.4 Swing components
24.Drag and Drop:JList and ListDrag and Drop:JList and List
25.DnD (drag and drop)JTree code DnD (drag and drop)JTree code
26.Drop: TextAreaDrop: TextArea
27.Drag and drop: TextArea 2Drag and drop: TextArea 2
28.Label DnD (Drag and Drop) Label DnD (Drag and Drop)
29.LabelDnD2 allows dropping color onto the foreground of the JLabelLabelDnD2 allows dropping color onto the foreground of the JLabel
30.Drag List Demo Drag List Demo
31.Drag Picture Demo
32.Extended DnD (Drag and Drop) DemoExtended DnD (Drag and Drop) Demo
33.Drag Color DemoDrag Color Demo
34.Drag File DemoDrag File Demo
35.Drag Picture Demo 2
36.Drag Color TextField DemoDrag Color TextField Demo
37.BasicDnD (Drag and Drop)BasicDnD (Drag and Drop)
38.Drag and drop icons: use an icon property.
39.Implement drag & drop functionality in your application
40.Detect a drag initiating gesture in your application
41.Making a Component Draggable
42.Getting and Setting Text on the System Clipboard
43.Use drag and drop to reorder a list
44.implements DragGestureListener, Transferable
45.Comma separated text will be inserted into two or more rows.
46.TransferHandler subclass wraps another TransferHandler and delegates most of its operations to the wrapped handler
47.Setting text drag in a JTextArea
48.Built-in drag and drop support: utilize a TransferHandler class
49.This program demonstrates drag and drop in an image list.
50.This program demonstrates the basic Swing support for drag and drop.