Tree目录实现拖拽功能演示实例(DragAndDrop)

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DragGestureEvent;
import java.awt.dnd.DragGestureListener;
import java.awt.dnd.DragSource;
import java.awt.dnd.DragSourceContext;
import java.awt.dnd.DragSourceDragEvent;
import java.awt.dnd.DragSourceDropEvent;
import java.awt.dnd.DragSourceEvent;
import java.awt.dnd.DragSourceListener;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetEvent;
import java.awt.dnd.DropTargetListener;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;

public class DragAndDrop extends JFrame {

	private static final long serialVersionUID = 1L;
	JScrollPane jScrollPane1 = new JScrollPane();
	JTextArea jTextArea1 = new JTextArea();

	public DragAndDrop() {
		try {
			jbInit();
		} catch (Exception exception) {
			exception.printStackTrace();
		}

		jScrollPane1.getViewport().setBackground(new Color(105, 38, 125));
		jTextArea1.setBackground(Color.orange);
		jTextArea1.setToolTipText("????");
		JTree jtr = new JTree();
		jtr.setBackground(Color.BLUE);
		jScrollPane1.getViewport().add(jtr);
		this.getContentPane().add(jTextArea1, BorderLayout.NORTH);
		this.getContentPane().add(jScrollPane1, BorderLayout.CENTER);

		DragSource dragSource = DragSource.getDefaultDragSource(); //创建拖拽源
		dragSource.createDefaultDragGestureRecognizer(jtr, DnDConstants.ACTION_COPY_OR_MOVE, new DragAndDropDragGestureListener()); //建立拖拽源和事件的联系
		new DropTarget(jTextArea1, new DragAndDropDropTargetListener());

	}

	private void jbInit() throws Exception {

	}

	public static void main(String[] args) {

		DragAndDrop dad = new DragAndDrop();
		dad.setTitle("拖拽演示");
		dad.setSize(400, 300);
		dad.setVisible(true);

	}
}

class DragAndDropDragGestureListener implements DragGestureListener {
	public void dragGestureRecognized(DragGestureEvent dge) {
		//将数据存储到Transferable中,然后通知组件开始调用startDrag()初始化
		JTree tree = (JTree) dge.getComponent();
		TreePath path = tree.getSelectionPath();
		if (path != null) {
			DefaultMutableTreeNode selection = (DefaultMutableTreeNode) path.getLastPathComponent();
			DragAndDropTransferable dragAndDropTransferable = new DragAndDropTransferable(selection);
			dge.startDrag(DragSource.DefaultCopyDrop, dragAndDropTransferable, new DragAndDropDragSourceListener());
		}
	}
}

class DragAndDropTransferable implements Transferable {
	private DefaultMutableTreeNode treeNode;

	DragAndDropTransferable(DefaultMutableTreeNode treeNode) {
		this.treeNode = treeNode;
	}

	static DataFlavor flavors[] = { DataFlavor.stringFlavor };

	public DataFlavor[] getTransferDataFlavors() {
		return flavors;
	}

	public boolean isDataFlavorSupported(DataFlavor flavor) {
		if (treeNode.getChildCount() == 0) {
			return true;
		}
		return false;
	}

	public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {

		return treeNode;

	}

}

class DragAndDropDragSourceListener implements DragSourceListener {
	public void dragDropEnd(DragSourceDropEvent dragSourceDropEvent) {
		if (dragSourceDropEvent.getDropSuccess()) {
			//拖拽动作结束的时候打印出移动节点的字符串
			int dropAction = dragSourceDropEvent.getDropAction();
			if (dropAction == DnDConstants.ACTION_MOVE) {
				System.out.println("MOVE: remove node");
			}
		}
	}

	public void dragEnter(DragSourceDragEvent dragSourceDragEvent) {
		DragSourceContext context = dragSourceDragEvent.getDragSourceContext();
		int dropAction = dragSourceDragEvent.getDropAction();
		if ((dropAction & DnDConstants.ACTION_COPY) != 0) {
			context.setCursor(DragSource.DefaultCopyDrop);
		} else if ((dropAction & DnDConstants.ACTION_MOVE) != 0) {
			context.setCursor(DragSource.DefaultMoveDrop);
		} else {
			context.setCursor(DragSource.DefaultCopyNoDrop);
		}
	}

	public void dragExit(DragSourceEvent dragSourceEvent) {
	}

	public void dragOver(DragSourceDragEvent dragSourceDragEvent) {
	}

	public void dropActionChanged(DragSourceDragEvent dragSourceDragEvent) {
	}
}

class DragAndDropDropTargetListener implements DropTargetListener {
	public void dragEnter(DropTargetDragEvent dtde) {

	}

	public void dragOver(DropTargetDragEvent dtde) {
	}

	public void dropActionChanged(DropTargetDragEvent dtde) {
	}

	public void dragExit(DropTargetEvent dte) {
	}

	public void drop(DropTargetDropEvent dtde) {
		Transferable tr = dtde.getTransferable();//使用该函数从Transferable对象中获取有用的数据
		String s = "";
		try {
			if (tr.isDataFlavorSupported(DataFlavor.stringFlavor)) {
				s = tr.getTransferData(DataFlavor.stringFlavor).toString();
			}
		} catch (Exception e) {

		}
		System.out.println(s);
		DropTarget c = (DropTarget) dtde.getSource();
		JTextArea d = (JTextArea) c.getComponent();
		if (s != null && s != "") {
			d.append(s + "\n");
		}
	}

}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值