转自:http://blog.csdn.net/awaysrain/services/trackbacks/18402.aspx
最近因为在一个项目中要实现JTree之间的拖拽,刚刚学习开始学习Java,参照网上的代码完成的,现在贴出来共享,希望对和我一样的菜鸟有点帮助,有什么错误的话还请多多指正。
DragSourceTree.java
package aabbcc;
import javax.swing.JTree;
import java.awt.dnd.DragGestureListener;
import java.awt.dnd.DragSourceListener;
import java.awt.dnd.DragSourceDragEvent;
import java.awt.dnd.DragSourceEvent;
import java.awt.dnd.DragSourceDropEvent;
import java.awt.dnd.DragGestureEvent;
import java.awt.dnd.DragSource;
import javax.swing.tree.DefaultMutableTreeNode;
import java.awt.datatransfer.StringSelection;
/**
* <p>Title: JTree之间的拖拽中的DragSourceTree</p>
* <p>Description:通过向从“源树”中拖拽结点在目标树中重新构造一棵树,实现JTree之间的拖拽单向拖拽,这个类是“源树”。 </p>
* @author awaysrain(绝对零度)
* @version 1.0
*/
public class DragSourceTree
extends JTree
implements DragGestureListener, DragSourceListener {
private DragSource dragSource = null;
/**
* Construct the source tree
*/
public DragSourceTree() {
super();
dragSource = DragSource.getDefaultDragSource();
dragSource.createDefaultDragGestureRecognizer(this,
java.awt.dnd.DnDConstants.
ACTION_COPY_OR_MOVE, this);
}
/**
* Construct the source tree
* @param treeNode DefaultMutableTreeNode
*/
public DragSourceTree(DefaultMutableTreeNode treeNode) {
super(treeNode);
dragSource = DragSource.getDefaultDragSource();
dragSource.createDefaultDragGestureRecognizer(this,
java.awt.dnd.DnDConstants.
ACTION_COPY_OR_MOVE, this);