Drag and drop: TextArea : Drag Drop « Swing JFC « Java






Drag and drop: TextArea

Drag and drop: TextArea
  
/*
Java Swing, 2nd Edition
By Marc Loy, Robert Eckstein, Dave Wood, James Elliott, Brian Cole
ISBN: 0-596-00408-7
Publisher: O'Reilly 
*/
// DropTest2.java
//Another simple drag & drop tester application.
//

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.dnd.DnDConstants;
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.InputStream;
import java.io.InputStreamReader;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;

public class DropTest2 extends JFrame implements DropTargetListener {

  DropTarget dt;

  JTextArea ta;

  public DropTest2() {
    super("Drop Test");
    setSize(300, 300);


    getContentPane().add(
        new JLabel("Drop a list from your file chooser here:"),
        BorderLayout.NORTH);
    ta = new JTextArea();
    ta.setBackground(Color.white);
    getContentPane().add(ta, BorderLayout.CENTER);

    // Set up our text area to recieve drops...
    // This class will handle drop events
    dt = new DropTarget(ta, this);
    setVisible(true);
  }

  public void dragEnter(DropTargetDragEvent dtde) {
    System.out.println("Drag Enter");
  }

  public void dragExit(DropTargetEvent dte) {
    System.out.println("Drag Exit");
  }

  public void dragOver(DropTargetDragEvent dtde) {
    System.out.println("Drag Over");
  }

  public void dropActionChanged(DropTargetDragEvent dtde) {
    System.out.println("Drop Action Changed");
  }

  public void drop(DropTargetDropEvent dtde) {
    try {
      // Ok, get the dropped object and try to figure out what it is
      Transferable tr = dtde.getTransferable();
      DataFlavor[] flavors = tr.getTransferDataFlavors();
      for (int i = 0; i < flavors.length; i++) {
        System.out.println("Possible flavor: "
            + flavors[i].getMimeType());
        // Check for file lists specifically
        if (flavors[i].isFlavorJavaFileListType()) {
          // Great! Accept copy drops...
          dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
          ta.setText("Successful file list drop.\n\n");

          // And add the list of file names to our text area
          java.util.List list = (java.util.List) tr
              .getTransferData(flavors[i]);
          for (int j = 0; j < list.size(); j++) {
            ta.append(list.get(j) + "\n");
          }

          // If we made it this far, everything worked.
          dtde.dropComplete(true);
          return;
        }
        // Ok, is it another Java object?
        else if (flavors[i].isFlavorSerializedObjectType()) {
          dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
          ta.setText("Successful text drop.\n\n");
          Object o = tr.getTransferData(flavors[i]);
          ta.append("Object: " + o);
          dtde.dropComplete(true);
          return;
        }
        // How about an input stream?
        else if (flavors[i].isRepresentationClassInputStream()) {
          dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
          ta.setText("Successful text drop.\n\n");
          ta.read(new InputStreamReader((InputStream) tr
              .getTransferData(flavors[i])),
              "from system clipboard");
          dtde.dropComplete(true);
          return;
        }
      }
      // Hmm, the user must not have dropped a file list
      System.out.println("Drop failed: " + dtde);
      dtde.rejectDrop();
    } catch (Exception e) {
      e.printStackTrace();
      dtde.rejectDrop();
    }
  }

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

           
         
    
  








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 capabilities: JListDrag capabilities: JList
22.A simple drop tester application for JDK 1.4 Swing componentsA simple drop tester application for JDK 1.4 Swing components
23.Drag and Drop:JList and ListDrag and Drop:JList and List
24.DnD (drag and drop)JTree code DnD (drag and drop)JTree code
25.Drop: TextAreaDrop: TextArea
26.Drag and drop: TextArea 2Drag and drop: TextArea 2
27.Label DnD (Drag and Drop) Label DnD (Drag and Drop)
28.LabelDnD2 allows dropping color onto the foreground of the JLabelLabelDnD2 allows dropping color onto the foreground of the JLabel
29.Drag List Demo Drag List Demo
30.Drag Picture Demo
31.Extended DnD (Drag and Drop) DemoExtended DnD (Drag and Drop) Demo
32.Drag Color DemoDrag Color Demo
33.Drag File DemoDrag File Demo
34.Drag Picture Demo 2
35.Drag Color TextField DemoDrag Color TextField Demo
36.BasicDnD (Drag and Drop)BasicDnD (Drag and Drop)
37.Drag and drop icons: use an icon property.
38.Implement drag & drop functionality in your application
39.Detect a drag initiating gesture in your application
40.Making a Component Draggable
41.Getting and Setting Text on the System Clipboard
42.Use drag and drop to reorder a list
43.Create a drag source a drop target and a transferable object.
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.