⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dndtree.java

📁 JTREE的例子
💻 JAVA
字号:
package dragTree;import javax.swing.*;import javax.swing.tree.*;import javax.swing.event.*;import java.awt.*;import java.awt.datatransfer.*;import java.awt.dnd.*;import java.awt.dnd.peer.*;import java.awt.event.*;import java.awt.datatransfer.*;import java.io.*;import util3.DMTreeNode;public class DnDTree extends JTree implements DragGestureListener{  private TreePath dragingTP;  private DragSource dragSource;  private DragSourceImpl dsi=new DragSourceImpl();  private DMTreeNode currenParent=null;  private boolean dropAble=false;  public DnDTree(DMTreeNode dmTreeNode) {    super(dmTreeNode);    currenParent=(DMTreeNode)      this.getPathForRow(0).getLastPathComponent();    dragSource=DragSource.getDefaultDragSource();    DragGestureRecognizer dgr=dragSource.createDefaultDragGestureRecognizer(        this,DnDConstants.ACTION_COPY_OR_MOVE,this);    dgr.setSourceActions(dgr.getSourceActions() & ~InputEvent.BUTTON3_MASK);    DropTarget dropTarget = new DropTarget(this,new DropTargetImpl());  }  public void dragGestureRecognized(DragGestureEvent e){    Point p=e.getDragOrigin();    Cursor cursor=null;    try{      dragingTP = getPathForLocation(p.x,p.y);    }catch(NullPointerException npe){return;}    if(dragingTP!=null){//System.out.println("here");      Transferable transferable = (Transferable)          (((DMTreeNode)dragingTP.getLastPathComponent()).getUserObject());      cursor = DragSource.DefaultCopyNoDrop;      int action = e.getDragAction();      if (action == DnDConstants.ACTION_MOVE){        this.dropAble=true;        cursor = DragSource.DefaultMoveDrop;      }      else if(action==DnDConstants.ACTION_COPY){        if(!((DMTreeNode)dragingTP.getLastPathComponent()).isLeaf()){          this.dropAble=false;          cursor=DragSource.DefaultCopyNoDrop;        }        else{          this.dropAble=true;          cursor=DragSource.DefaultCopyDrop;        }      }      dragSource.startDrag(e, cursor, transferable, dsi);    }  }  public void addNode(String name){    TransferableImpl ti=new TransferableImpl(name);    DMTreeNode tn=null;    this.currenParent.add(tn=new DMTreeNode(ti));    this.currenParent=tn;  }  class DropTargetImpl extends DropTargetAdapter{    public void dragOver(DropTargetDragEvent e) {    }    public void drop(DropTargetDropEvent e) {//System.out.println("here0");      try{        Transferable tr = e.getTransferable();        if (!tr.isDataFlavorSupported(TransferableImpl.TRAN_FLAVOR)) e.rejectDrop();        TransferableImpl transfer = (TransferableImpl)tr.getTransferData(TransferableImpl.TRAN_FLAVOR);        Point p=e.getLocation();        TreePath toTP = getPathForLocation(p.x, p.y);        //whether it can be droped here?        if(!(isDropOK(dragingTP,toTP) && dropAble)) return;        boolean isCopyDrop = DnDConstants.ACTION_COPY==e.getDropAction();        DMTreeNode toParent=(DMTreeNode)toTP.getLastPathComponent();        DMTreeNode dragingTN=null;        DMTreeNode oldParent=null;        int oldIndex=0;        int newIndex=0;        if(isCopyDrop){          TransferableImpl tranCopy=transfer.copy();          dragingTN=new DMTreeNode(tranCopy);        }else{          oldParent=(DMTreeNode)dragingTP.getParentPath().getLastPathComponent();          DMTreeNode tn=(DMTreeNode)dragingTP.getLastPathComponent();          oldIndex=oldParent.getIndex(tn);          newIndex=toParent.getParent().getIndex(toParent);          oldParent.remove(tn);          dragingTN=tn;        }        if(!toParent.isLeaf()){          toParent.add(dragingTN);        }else{          DMTreeNode parent=(DMTreeNode)toParent.getParent();          int index=parent.getIndex(toParent);          //System.out.println(parent.getChildCount());          if(parent==oldParent && newIndex>oldIndex)index++;          parent.insert(dragingTN,index);        }        DnDTree.this.updateUI();      }catch(Exception ex){        ex.printStackTrace();        e.rejectDrop();      }    }    boolean isDropOK(TreePath fromTP,TreePath toTP){      if(fromTP==null || toTP==null) {/*System.out.println("cant0");*/ return false;}      if(fromTP.equals(toTP)) {/*System.out.println("cant1");*/ return false;}      if(fromTP.isDescendant(toTP)) {/*System.out.println("cant2");*/ return false;}      return true;    }  }  class DragSourceImpl extends DragSourceAdapter{  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -