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

JSplitPane doesn't accept drop

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P5 P5
    • None
    • 1.4.0
    • client-libs
    • x86
    • windows_nt



      Name: jl125535 Date: 12/13/2001


      java version "1.3.1_01"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.1_01)
      Java HotSpot(TM) Client VM (build 1.3.1_01, mixed mode)

      I'm trying to use drag and drop to move components around in a split pane.
      simple example would be to drag the top component into the bottom part of
      the split pane and vice versa (NOT DATATRANSFER, but Component transfer,
      although i have verified that even data transfer doesn't work)
      My custom extension of the JSplitPane (which has both a DragSource and
      a DropTarget instantiated) doesn't respond to DropEvents although it does
      trigger DragEvents.

      Steps to reproduce:
      1. Compile DndTest.java (given below)
      2. Run DndTest
      3. In the frame that appears, drag the "Top Component 1" over the
         "Bottom Component 1".

      Expected behavior:

      The console should show these messages.
      (JDK 1.4 beta 3 works as expected.)

      javax.swing.JLabel[,1,1,132x17,alignmentX=0.0,alignmentY=null,border=,flags=0,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,horizontalAlignment=LEADING,horizontalTextPosition=TRAILING,iconTextGap=4,labelFor=,text=Top Component 1,verticalAlignment=CENTER,verticalTextPosition=CENTER]
      DropTargetListener: dragEnter
      DropTargetListener: dragOver
      DropTargetListener: dragOver
      [...]
      DropTargetListener: dragOver
      DropTargetListener: drop
      Drop rejected
      DragSourceListener: dragDropEnd


      Actual behavior:

      On Solaris 8 or Windows 2000 with JDK 1.3.1, the console only
      shows these messages.

      javax.swing.JLabel[,1,1,132x17,alignmentX=0.0,alignmentY=null,border=,flags=0,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,horizontalAlignment=LEADING,horizontalTextPosition=TRAILING,iconTextGap=4,labelFor=,text=Top Component 1,verticalAlignment=CENTER,verticalTextPosition=CENTER]
      DragSourceListener: dragDropEnd

      The DragListener does not receive the dragEnter, dragOver, or drop messages.


      Source code:

      ----------------------------------------------------------------------------
      import javax.swing.*;
      import java.awt.dnd.*;
      import java.awt.datatransfer.*;
      import java.awt.*;
      import java.awt.event.WindowAdapter;
      import java.awt.event.WindowEvent;

      public class DnDTest extends JFrame {
        public DnDTest(String psTitle) {
          super(psTitle);
        }

        public static void main(String[] args) {
          DnDTest testFrame = new DnDTest("DnD Test");
          testFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
          testFrame.addWindowListener(
              new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                  System.exit(0);
                }
          });
          DnDSplitPane splitPane1 = new DnDSplitPane(JSplitPane.VERTICAL_SPLIT);
          splitPane1.setTopComponent(new JLabel("Top Component 1"));
          splitPane1.setBottomComponent(new JLabel("Bottom Component 1"));
          //splitPane1.setDividerLocation(50);
          testFrame.getContentPane().add(splitPane1, BorderLayout.CENTER);
      /*
          DnDSplitPane splitPane2 = new DnDSplitPane(JSplitPane.VERTICAL_SPLIT);
          splitPane2.setTopComponent(new JLabel("Top Component 2"));
          splitPane2.setBottomComponent(new JLabel("Bottom Component 2"));
          testFrame.getContentPane().add(splitPane2, BorderLayout.SOUTH);
      */
          testFrame.pack();
          testFrame.setVisible(true);
        }
      }

      class DnDSplitPane extends JSplitPane implements DragGestureListener {

        public DnDSplitPane() {
          this(JSplitPane.VERTICAL_SPLIT);
        }

        public DnDSplitPane(int newOrientation) {
          super(newOrientation);

          // Drag 'n' Drop enabling
          dragSource = new DragSource();
          dspdsListener = new DnDSplitPaneDragSourceListener();
          dragSource.createDefaultDragGestureRecognizer(this,
                                                        DnDConstants.ACTION_MOVE,
                                                        this);
          dspdtListener = new DnDSplitPaneDropTargetListener();
          dropTarget = new DropTarget(this, dspdtListener);
        }

        public void dragGestureRecognized(DragGestureEvent dge) {

          Component loComponent = findComponentAt(dge.getDragOrigin());
          System.out.println(loComponent.toString());

          if (loComponent == null) {
            System.out.println("No selection");
            return;
          }

          //PanelContainerSelection loPcs = new PanelContainerSelection(loComponent);
          StringSelection loPcs = new
      StringSelection(((JLabel)loComponent).getText());
          dragSource.startDrag(dge, DragSource.DefaultMoveDrop, loPcs, dspdsListener);
        }

        class DnDSplitPaneDragSourceListener implements DragSourceListener {
          /**
           * This is where the DualListSelectionComponent does the actual moving
           * of data between its two lists
           */
          public void dragDropEnd(DragSourceDropEvent dsde) {
            System.out.println("DragSourceListener: dragDropEnd");
            if (dsde.getDropSuccess())
              System.out.println("success");
          }

          public void dragEnter(DragSourceDragEvent dsde) {
            //System.out.println("DragSourceListener: dragEnter");
          }

          public void dragExit(DragSourceEvent dse) {
            //System.out.println("DragSourceListener: dragExit");
          }

          public void dragOver(DragSourceDragEvent dsde) {
            //System.out.println("DragSourceListener: dragOver");
          }

          public void dropActionChanged(DragSourceDragEvent dsde) {
            //System.out.println("DragSourceListener: dropActionChanged");
          }
        }

        class DnDSplitPaneDropTargetListener implements DropTargetListener {
          public void dragEnter(DropTargetDragEvent dtde) {
            System.out.println("DropTargetListener: dragEnter");
            dtde.acceptDrag(DnDConstants.ACTION_MOVE);
          }

          public void dragExit(DropTargetEvent dte) {
            System.out.println("DropTargetListener: dragExit");
          }

          public void dragOver(DropTargetDragEvent dtde) {
            System.out.println("DropTargetListener: dragOver");
          }

          public void drop(DropTargetDropEvent dtde) {
            System.out.println("DropTargetListener: drop");

            Transferable transferable = dtde.getTransferable();

            try {
            if (transferable.isDataFlavorSupported(
                  new DataFlavor(DataFlavor.javaJVMLocalObjectMimeType))) {
              dtde.acceptDrop(DnDConstants.ACTION_MOVE);
              Component loNewComponent = (Component)transferable.getTransferData(new
      DataFlavor(DataFlavor.javaJVMLocalObjectMimeType));
              Component loOldComponent = findComponentAt(dtde.getLocation());
              if (getTopComponent().equals(loOldComponent)) {
                setTopComponent(loNewComponent);
                setBottomComponent(loOldComponent);
              } else {
                setBottomComponent(loNewComponent);
                setTopComponent(loOldComponent);
              }
              dtde.getDropTargetContext().dropComplete(true);
            }
            else{
              System.out.println("Drop rejected");
              dtde.rejectDrop();
            }
            } catch (Exception e) {
              e.printStackTrace();
            }
          }

          public void dropActionChanged(DropTargetDragEvent dtde) {
            System.out.println("DropTargetListener: dragEnter");
          }
        }

        private DragSource dragSource = null;
        private DnDSplitPaneDragSourceListener dspdsListener = null;
        private DropTarget dropTarget = null;
        private DnDSplitPaneDropTargetListener dspdtListener = null;
      }

      class ComponentSelection implements Transferable {
        public ComponentSelection(Component loPc) {
          transferData = loPc;
        }

        public Object getTransferData(DataFlavor flavor) {
          if (flavor.isMimeTypeEqual(DataFlavor.javaJVMLocalObjectMimeType)) {
            return transferData;
          }
          return null;
        }

        public DataFlavor[] getTransferDataFlavors() {
          if (supportedFlavors == null) {
            try {
              supportedFlavors = new DataFlavor[]
                { new DataFlavor(DataFlavor.javaJVMLocalObjectMimeType) };
            } catch (ClassNotFoundException e) {
              e.printStackTrace();
            }
          }
          return supportedFlavors;
        }

        public boolean isDataFlavorSupported(DataFlavor flavor) {
          return flavor.isMimeTypeEqual(DataFlavor.javaJVMLocalObjectMimeType);
        }

        private Component transferData;
        private static DataFlavor[] supportedFlavors = null;
      }
      ----------------------------------------------------------------------------

      It prints out the drag events and drag gesture happening, and the
      the dragDropEnd() method at the end of the drag and drop, but doesn't
      seem to ever enter the drop method.

      Any help is appreciated.
      Thanks,
      Nilesh
      (Review ID: 137000)
      ======================================================================

            dassunw Das Das (Inactive)
            jleesunw Jon Lee (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: