是这样。结合起来 jTree。setDropTarget(new DropTarget(jTree,DndDConstants.Action_COPY,new DropTargetWork())
这个DropTargetWork implements DropTargetListener

解决方案 »

  1.   

    现在在树上,valueChanged()和dragGestureRecognized()两个方法有反应
    其他的就没有了
    请帮看一下程序,哪里有遗漏呢?
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.plaf.metal.*;
    import javax.swing.tree.*;import java.awt.*;
    import java.awt.datatransfer.*;
    import java.awt.dnd.*;
    import java.awt.dnd.peer.*;
    import java.awt.event.*;
    import java.io.*;public class ResDnDJTree extends JTree
                      implements TreeSelectionListener,
                      DragGestureListener, DropTargetListener,
                      DragSourceListener {  /** Variables needed for DnD */
      private DragSource dragSource = null;
      private DragSourceContext dragSourceContext = null;
      /** Constructor */
      public ResDnDJTree(TreeNode root) {
        super(root);    //Add TreeSelectionListener
        addTreeSelectionListener(this);    dragSource = DragSource.getDefaultDragSource() ;    //Add DragGestureListener
        DragGestureRecognizer dgr =
          dragSource.createDefaultDragGestureRecognizer(
            this,                             //DragSource
            DnDConstants.ACTION_COPY_OR_MOVE, //specifies valid actions
            this                              //DragGestureListener
          );    /* Eliminates right mouse clicks as valid actions - useful especially
         * if you implement a JPopupMenu for the JTree
         */
        dgr.setSourceActions(dgr.getSourceActions() & ~InputEvent.BUTTON3_MASK);    /* First argument:  Component to associate the target with
         * Second argument: DropTargetListener
         */
        //Add DropTargetListener
        DropTarget dropTarget = new DropTarget(this, this);
        this.setDropTarget( dropTarget );
      }  public ResDnDJTree(TreeModel tm) {
        this((TreeNode)tm.getRoot());
      }
      ///////////////////////// Interface stuff ////////////////////  /** DragGestureListener interface method */
      public void dragGestureRecognized(DragGestureEvent e) {
        System.out.println( "Enter DragGestureListener.dragGestureRecognized()" );
      }  /** DragSourceListener interface method */
      public void dragDropEnd(DragSourceDropEvent dsde) {
        System.out.println("Enter DragSourceListener.dragDropEnd()");
      }  /** DragSourceListener interface method */
      public void dragEnter(DragSourceDragEvent dsde) {
        System.out.println("Enter DragSourceListener.dragEnter()");
      }  /** DragSourceListener interface method */
      public void dragOver(DragSourceDragEvent dsde) {
        System.out.println("Enter DragSourceListener.dragOver()");
      }  /** DragSourceListener interface method */
      public void dropActionChanged(DragSourceDragEvent dsde) {
        System.out.println("Enter DragSourceListener.dropActionChanged()");
      }  /** DragSourceListener interface method */
      public void dragExit(DragSourceEvent dsde) {
        System.out.println("Enter DragSourceListener.dragExit()");
      }
      /** DropTargetListener interface method - What we do when drag is released */
      public void drop(DropTargetDropEvent e) {
        System.out.println("Enter DropTargetListener.drop()");
      } //end of method  /** DropTaregetListener interface method */
      public void dragEnter(DropTargetDragEvent e) {
        System.out.println("Enter DropTaregetListener.dragEnter()");
      }  /** DropTaregetListener interface method */
      public void dragExit(DropTargetEvent e) {
        System.out.println("Enter DropTaregetListener.dragExit()");
      }  /** DropTaregetListener interface method */
      public void dragOver(DropTargetDragEvent e) {
        System.out.println("Etner DropTaregetListener.dragOver()");
      }  /** DropTaregetListener interface method */
      public void dropActionChanged(DropTargetDragEvent e) {
        System.out.println("Enter DropTaregetListener.dropActionChanged()");
      }
      /** TreeSelectionListener - sets selected node */
      public void valueChanged(TreeSelectionEvent evt) {
        System.out.println("Etner TreeSelectionListener.valueChanged()");
      }
    } //end of DnDJTree