Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4349345

Drag and Drop works, but cursor always indicate illegal operation

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P3 P3
    • None
    • 1.2.2, 1.2.2_005
    • client-libs
    • x86
    • windows_nt, windows_2000

      I have a customer that reports:

      I've successfully implemented Drag and Drop in a couple of java applications, but

      ****the cursor always shows the 'not-droppable/illegal operation'.****

      Even some code I got straight from the Java1.2 Unleashed CD had the same problem. So I put
      together a base case: JFrame with 2 JPanels, one small Jpanel placed on left panel is
      draggable to right panel. When you start dragging the small panel, and while still in left
      panel, the cursor flickers very briefly to the correct hand image. Then just as the right
      panel (droppable region) is entered, the cursor flickers for an instant to the correct
      hand-with-plus image.

      import java.awt.*;
      import java.awt.event.*;
      import java.awt.dnd.*;
      import java.awt.datatransfer.*;
      import java.io.*;
      import javax.swing.*;

      public class TestDnD extends JFrame {
      static TestDnD drageFrame1;
      JPanel left = new JPanel();
      JPanel right = new JPanel();

      MyDraggable mydrag = new MyDraggable();
      DataFlavor myFlavor = new DataFlavor(mydrag.getClass(), "mydraggable");
      MyDragSource source = new MyDragSource();
      DropTarget target = new DropTarget(right, DnDConstants.ACTION_COPY,
                                           new DropTargetHandler(),
                                           true);

      ButtonTransfer transferable = new ButtonTransfer();
      DragSourceHandler sourceHandler = new DragSourceHandler();


      public TestDnD() {
      setSize(400,200);
      setLocation(200,200);
      getContentPane().setLayout(new GridLayout(1,2,3,3));

      mydrag.setBounds(10,10,40,40);
      mydrag.setBackground(Color.red);

      left.setLayout(null);
      left.setBackground(Color.blue);
      left.add(mydrag);

      right.setBackground(new Color(70, 50,170));
      right.setLayout(null);

      target.setActive(true);
      Toolkit toolkit = Toolkit.getDefaultToolkit();
      try {
      toolkit.createDragGestureRecognizer(Class.forName("java.awt.dnd.MouseDragGestureRecognizer"),
      source,
      mydrag,
      DnDConstants.ACTION_COPY,
      new DragHandler());

      } catch(ClassNotFoundException ex) {
      System.out.println("Recognizer class not found.");
      System.exit(0);
      }

      getContentPane().setBackground(Color.black);
      getContentPane().add(left, 0);
      getContentPane().add(right,1);
      setVisible(true);
      }

      public static void main(String[] args) {
      drageFrame1 = new TestDnD();
      }

      /**
      ** Allow for window closing
      **/
      protected void processWindowEvent(WindowEvent e) {
      super.processWindowEvent(e);
      if (e.getID() == WindowEvent.WINDOW_CLOSING) {
      System.exit(0);
      }
      }


      /**
      ** Drag and Drop inner classes -------------------------------------------
      **/

      /**
      ** Serializable required by JDK1.2.2, but not by JBuilder 3
        **/
      // --------------------------------------------------------
       
        class DragSourceHandler implements DragSourceListener, Serializable {
      public void dropActionChanged(DragSourceDragEvent ev) {
      System.out.println("Source: Drop action changed");
      }

      public void dragEnter(DragSourceDragEvent ev) {
      System.out.println("Source: Drag enter");
      }

      public void dragOver(DragSourceDragEvent ev) {
      }

      public void dragExit(DragSourceEvent ev) {
      System.out.println("Source: Drag exit");
      }

      public void dragDropEnd(DragSourceDropEvent ev) {
      System.out.println("Source: Drag drop end");
      }
      }


      /**
      ** Serializable required by JDK1.2.2, but not by JBuilder 3
        **/
      // --------------------------------------------------------

      class DropTargetHandler implements DropTargetListener, Serializable {
      public void dragEnter(DropTargetDragEvent ev) {
      // Tried accepting all by default by commenting out all but:
      // ev.acceptDrag(DnDConstants.ACTION_COPY);
      // but still didn't work
      // ---------------------------------------------------------
      System.out.println ("Target: Drag enter");
      DataFlavor df[] = ev.getCurrentDataFlavors();
      for (int i = 0; i < df.length; i++) {
      if (df[i].equals(myFlavor)) {
      ev.acceptDrag(DnDConstants.ACTION_COPY);
      return;
      }
      }
      ev.rejectDrag();
      }

      public void dragOver(DropTargetDragEvent ev) {
      }

      public void dragExit(DropTargetEvent ev) {
      System.out.println ("Target: Drag exit");
      }

      public void dropActionChanged(DropTargetDragEvent ev) {
      System.out.println("Target: Drop action changed");
      }

      public void drop(DropTargetDropEvent ev) {
      System.out.println ("Target: Dropped");
      ev.acceptDrop (DnDConstants.ACTION_COPY);
      Transferable transfer = ev.getTransferable();
      DataFlavor df[] = ev.getCurrentDataFlavors();
      String input = "";
      JButton tempButton = null;
      MyDraggable md = null;

      System.out.println("DropTargetHandler: drop(): try completing drop...");

      try {
      // For each dataflavor supported in this drag operation...
      for (int i=0; i<df.length; i++) {
      if (df[i].equals(myFlavor)) {
      System.out.println("DropTargetHandler: drop(): Button flavor found, create temp button");
      md = new MyDraggable();
      md.setBounds((int)ev.getLocation().getX(),
      (int)ev.getLocation().getY(),
      ((MyDraggable) transfer.getTransferData(df[i])).getWidth(),
      ((MyDraggable) transfer.getTransferData(df[i])).getHeight());
      md.setBackground(((MyDraggable) transfer.getTransferData(df[i])).getBackground());
      right.add(md);
      }
      }

      System.out.println("DropTargetHandler: drop(): Done adding button, repain...");
      drageFrame1.invalidate();
      drageFrame1.repaint();

      } catch (Exception e) {
      System.out.println(e.toString());
      e.printStackTrace();
      }

      try {
      System.out.println("DropTargetHandler: drop(): set dropComplete(true)");
      target.getDropTargetContext().dropComplete(true);
      } catch (Exception e) {
      }
      System.out.println("DropTargetHandler: drop(): exit drop()");
      }
      }


      /**
      ** Trying to implement drag and droppable buttons
      ** Serializable required by JDK1.2.2, but not by JBuilder 3
        **/
      // --------------------------------------------------------
      class ButtonTransfer implements Transferable, Serializable {
      public DataFlavor[] getTransferDataFlavors() {
      DataFlavor[] flavors = new DataFlavor[1];
      flavors[0] = myFlavor;
      return flavors;
      }

      public boolean isDataFlavorSupported(DataFlavor flavor) {
      return (flavor.equals(myFlavor));
      }

      public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
      return mydrag;
      }
      }


      // --------------------------------------------------
      class DragHandler implements DragGestureListener {
      public void dragGestureRecognized(DragGestureEvent e) {
      e.startDrag(new Cursor(Cursor.HAND_CURSOR),
      transferable,sourceHandler);
      }
      }


      /** Had to subclass this so I could make it serializable
      ** Oddly enough, in JBuilder3 and compiled JAR files generated
      ** by JBuilder3 didn't require this class, ButtonTransfer, DropTargetHandler,
      ** or DragSourceHandler to be serializable. But compiling with jdk1.2.2
      ** did, otherwise would throw NotSerializableException's
        **/
      class MyDragSource extends DragSource implements Serializable {
      }


      /**
      ** My draggable class
        **/
      class MyDraggable extends JPanel implements Serializable {
      public MyDraggable() {
      }
      }
      }


      Name: jk109818 Date: 07/07/2000


      java version "1.2.2"
      Classic VM (build JDK-1.2.2-001, native threads, symcjit)

      When implementing Drag and Drop, the cursor always displays a 'invalid drop
      target' cursor over a valid drop target ( specifically:
      sun.awt.windows.WCustomCursor[MoveNoDrop32x32] ).

        From my perspective, any attempt at implementing Drag and Drop will reproduce
      the problem. But I'll include a sample java source file which I grabbed from
      the 'Java1.2 Unleashed' CD. It has the same problem as all my applications, and
      it's fairly short.

      All attempts at setting the cursor manually when a dragEnter()/dragExit()
      occurs did nothing.

      <pre>
      import java.awt.*;
      import java.awt.event.*;
      import java.awt.dnd.*;
      import java.awt.datatransfer.*;
      import java.io.*;

      public class DragNDrop extends Frame {
       int screenWidth = 400;
       int screenHeight = 400;
       Panel panel = new Panel();
       Label topLabel = new Label("Enter text in this text area:");
       Label bottomLabel = new Label("And then drag it to this text area:");
       TextArea textArea1 = new TextArea();
       TextArea textArea2 = new TextArea();
       
       //Drag and drop variables
       DragSource source = new DragSource();
       DropTarget target = new DropTarget(textArea2,
        DnDConstants.ACTION_COPY,new DropTargetHandler(),true);
       TextTransfer transferable = new TextTransfer();
       DragSourceHandler sourceHandler = new DragSourceHandler();
       
       public static void main(String[] args) {
        DragNDrop app = new DragNDrop();
       }
       public DragNDrop() {
        super("DragNDrop");
        setup();
        setSize(screenWidth,screenHeight);
        addWindowListener(new WindowEventHandler());
        show();
       }
       void setup() {
        target.setActive(true);
        panel.setLayout(new GridLayout(4,1));
        panel.add(topLabel);
        panel.add(textArea1);
        Toolkit toolkit=Toolkit.getDefaultToolkit();
        try {
         toolkit.createDragGestureRecognizer(Class.forName(
          "java.awt.dnd.MouseDragGestureRecognizer"),source,textArea1,
          DnDConstants.ACTION_COPY,new DragHandler());
        }catch(ClassNotFoundException ex){
         System.out.println("Recognizer class not found.");
         System.exit(0);
        }
        panel.add(bottomLabel);
        panel.add(textArea2);
        add("Center",panel);
       }
       class DragSourceHandler implements DragSourceListener {
        public void dropActionChanged(DragSourceDragEvent ev) {
         System.out.println("Source: Drop action changed");
        }
        public void dragEnter(DragSourceDragEvent ev) {
         System.out.println("Source: Drag enter");
        }
        public void dragOver(DragSourceDragEvent ev) {
        }
        public void dragExit(DragSourceEvent ev) {
         System.out.println("Source: Drag exit");
       }
        public void dragDropEnd(DragSourceDropEvent ev) {
         System.out.println("Source: Drag drop end");
        }
       }
       class DropTargetHandler implements DropTargetListener {
        public void dragEnter(DropTargetDragEvent ev) {
         System.out.println ("Target: Drag enter");
         DataFlavor df[] = ev.getCurrentDataFlavors();
         for (int i = 0; i < df.length; i++) {
          if (df[i].equals (DataFlavor.plainTextFlavor) ||
      df[i].equals (DataFlavor.stringFlavor)) {
           ev.acceptDrag(DnDConstants.ACTION_COPY);
           return;
          }
         }
         ev.rejectDrag();
        }
        public void dragOver(DropTargetDragEvent ev) {
        }
        public void dragExit(DropTargetEvent ev) {
         System.out.println ("Target: Drag exit");
        }
        public void dropActionChanged(DropTargetDragEvent ev) {
         System.out.println("Target: Drop action changed");
        }
        public void drop(DropTargetDropEvent ev) {
         System.out.println ("Target: Dropped");
         ev.acceptDrop (DnDConstants.ACTION_COPY);
         Transferable transfer = ev.getTransferable();
         DataFlavor df[] = ev.getCurrentDataFlavors();
         String input = "";
         try {
          for (int i=0;i<df.length;i++) {
           if (df[i].equals(DataFlavor.stringFlavor) ||
            df[i].equals(DataFlavor.plainTextFlavor)) {
            input = (String) transfer.getTransferData(df[i]);
           }
          }
          textArea2.setText(input);
         }catch (Exception e) {
          System.out.println(e.toString());
         }
         try {
          target.getDropTargetContext().dropComplete(true);
         }catch (Exception e) {
         }
        }
       }
       class TextTransfer implements Transferable {
        public DataFlavor[] getTransferDataFlavors() {
         DataFlavor[] flavors = new DataFlavor[1];
         flavors[0] = DataFlavor.plainTextFlavor;
         return flavors;
        }
        public boolean isDataFlavorSupported(DataFlavor flavor) {
         return (flavor.equals(DataFlavor.plainTextFlavor));
        }
        public Object getTransferData(DataFlavor flavor)
          throws UnsupportedFlavorException, IOException {
         return textArea1.getText();
        }
       }
       class DragHandler implements DragGestureListener {
        public void dragGestureRecognized(DragGestureEvent e) {
         e.startDrag(new Cursor(Cursor.HAND_CURSOR),
           transferable,sourceHandler);
        }
       }
       class WindowEventHandler extends WindowAdapter {
        public void windowClosing(WindowEvent e){
         System.exit(0);
        }
       }
      }
      </pre>
      (Review ID: 104262)
      ======================================================================

            dmendenhsunw David Mendenhall (Inactive)
            duke J. Duke
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: