jtree可拖拽节点到textare:
package com.sysc.xmleditor.core; import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Point; 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.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.IOException; import javax.swing.JFrame; import javax.swing.JTextArea; import javax.swing.JTree; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.TreePath; public class JTreeTest { public static void main(String[] args) { //整体窗口的使用 final JFrame jFrame=new JFrame("我的第一个窗体"); jFrame.setSize(new Dimension(800, 500));//设置窗体的大小 jFrame.setBackground(Color.BLACK); jFrame.setLocation(new Point(300, 300));//设置窗体显示的位置 jFrame.setVisible(true); jFrame.setLayout(new FlowLayout()); jFrame.addWindowListener(new WindowAdapter() {//加入关闭窗体事件 @Override public void windowClosing(WindowEvent e) { // TODO Auto-generated method stub System.out.println("系统正在关闭"); System.exit(0); } }); DefaultMutableTreeNode root = new DefaultMutableTreeNode( " root ");//设置一个tree的根节点color DefaultMutableTreeNode color = new DefaultMutableTreeNode( " Color ");//设置一个tree的根节点color DefaultMutableTreeNode gray = new DefaultMutableTreeNode ( " Gray " );//设置另一个节点gray color.add (gray);//把Gary加入到color //设置各个节点子类、父类关系 DefaultMutableTreeNode red=new DefaultMutableTreeNode ( " Red " ); color.add (red ); DefaultMutableTreeNode Lightgray=new DefaultMutableTreeNode ( " Lightgray " ); gray.add (Lightgray ); DefaultMutableTreeNode Darkgray=new DefaultMutableTreeNode ( " Darkgray " ); gray.add (Darkgray ); DefaultMutableTreeNode Green=new DefaultMutableTreeNode ( " Green " ); color.add ( Green); root.add(color); JTree example5 = new JTree (root);//初始化jtree,把根节点color传入 jFrame.add(example5);//加入jframe面板 JTextArea textArea=new JTextArea(20,20); textArea.setBackground(Color.YELLOW); jFrame.add(textArea); DragSource dragSource=DragSource.getDefaultDragSource();//创建拖拽环境 //设置拖拽组件,拖拽方式,拖拽数据准备传输 dragSource.createDefaultDragGestureRecognizer(example5, DnDConstants.ACTION_COPY_OR_MOVE , new MyDemoDragGestureListener()); new DropTarget(textArea, new MyDemoDropTargetListener());//设置拖拽目的地的组件,上面所执行的操作 } } /** * 从被拖拽组件当中得到退拽数据 * @author Administrator * */ class MyDemoDragGestureListener implements DragGestureListener{ @Override public void dragGestureRecognized(DragGestureEvent dge) { // TODO Auto-generated method stub JTree tree = (JTree) dge.getComponent(); TreePath path = tree.getSelectionPath(); if (path != null) { DefaultMutableTreeNode selection = (DefaultMutableTreeNode) path .getLastPathComponent(); //开始拖拽,保存拖拽数据,以及拖拽时候的鼠标样式 dge.startDrag(DragSource.DefaultCopyDrop, new MyDemoTransferable(selection), new MyDemoDragSourceListener()); } } } /** * 暂时存放拖放数据的地方 * @author Administrator * */ class MyDemoTransferable implements Transferable { private DefaultMutableTreeNode treeNode; public MyDemoTransferable(DefaultMutableTreeNode treeNode){ this.treeNode=treeNode; } @Override public Object getTransferData(DataFlavor arg0) throws UnsupportedFlavorException, IOException { // TODO Auto-generated method stub return treeNode; } static DataFlavor flavors[] = { DataFlavor.stringFlavor }; @Override public DataFlavor[] getTransferDataFlavors() { // TODO Auto-generated method stub return flavors; } //是否支持指定数据flavors @Override public boolean isDataFlavorSupported(DataFlavor arg0) { // TODO Auto-generated method stub return true; } } /** * 设置拖拽鼠标样式的地方 * @author Administrator * */ class MyDemoDragSourceListener implements DragSourceListener{ @Override public void dragDropEnd(DragSourceDropEvent arg0) { // TODO Auto-generated method stub System.out.println("拖拽已完成"+arg0.getDropAction()); } @Override public void dragEnter(DragSourceDragEvent arg0) { // TODO Auto-generated method stub System.out.println("进入拖拽位置"+arg0.getDropAction()); DragSourceContext context = arg0.getDragSourceContext(); context.setCursor(DragSource.DefaultMoveDrop); } @Override public void dragExit(DragSourceEvent arg0) { // TODO Auto-generated method stub System.out.println("离开拖拽位置"); DragSourceContext context = arg0.getDragSourceContext(); context.setCursor(DragSource.DefaultCopyDrop); } @Override public void dragOver(DragSourceDragEvent arg0) { // TODO Auto-generated method stub } @Override public void dropActionChanged(DragSourceDragEvent arg0) { // TODO Auto-generated method stub } } /** * 拖拽目的地所执行的操作 * @author Administrator * */ class MyDemoDropTargetListener implements DropTargetListener{ @Override public void dragEnter(DropTargetDragEvent dtde) { // TODO Auto-generated method stub System.out.println("dsdsdsd"); dtde.getDropTargetContext().getComponent().setBackground(Color.GRAY); } @Override public void dragExit(DropTargetEvent dte) { // TODO Auto-generated method stub dte.getDropTargetContext().getComponent().setBackground(Color.YELLOW); } @Override public void dragOver(DropTargetDragEvent dtde) { // TODO Auto-generated method stub } @Override public void drop(DropTargetDropEvent dtde) { // TODO Auto-generated method stub Transferable tr = dtde.getTransferable();// 使用该函数从Transferable对象中获取有用的数据 String str = ""; try { if (tr.isDataFlavorSupported(DataFlavor.stringFlavor)) { str = tr.getTransferData(DataFlavor.stringFlavor).toString(); } } catch (IOException ex) { } catch (UnsupportedFlavorException ex) { } System.out.println(str); DropTarget target = (DropTarget) dtde.getSource(); JTextArea filed = (JTextArea) target.getComponent(); if (str != null && str != "") { filed.setText(filed.getText() + str); } dtde.getDropTargetContext().getComponent().setBackground(Color.YELLOW); System.out.println("*********end*************"); } @Override public void dropActionChanged(DropTargetDragEvent dtde) { // TODO Auto-generated method stub } }
jtree可退拽节点到自身节点下面:
package com.sysc.xmleditor.core.utils; import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Point; 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.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.IOException; import javax.swing.JFrame; import javax.swing.JTree; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.DefaultTreeModel; import javax.swing.tree.TreeNode; import javax.swing.tree.TreePath; public class JTreeTest2 { public static void main(String[] args) { //整体窗口的使用 final JFrame jFrame=new JFrame("我的第一个窗体"); jFrame.setSize(new Dimension(800, 500));//设置窗体的大小 jFrame.setBackground(Color.BLACK); jFrame.setLocation(new Point(300, 300));//设置窗体显示的位置 jFrame.setVisible(true); jFrame.setLayout(new FlowLayout()); jFrame.addWindowListener(new WindowAdapter() {//加入关闭窗体事件 @Override public void windowClosing(WindowEvent e) { // TODO Auto-generated method stub System.out.println("系统正在关闭"); System.exit(0); } }); DefaultMutableTreeNode root = new DefaultMutableTreeNode( " root ");//设置一个tree的根节点color DefaultMutableTreeNode color = new DefaultMutableTreeNode( " Color ");//设置一个tree的根节点color DefaultMutableTreeNode gray = new DefaultMutableTreeNode ( " Gray " );//设置另一个节点gray color.add (gray);//把Gary加入到color //设置各个节点子类、父类关系 DefaultMutableTreeNode red=new DefaultMutableTreeNode ( " Red " ); color.add (red ); DefaultMutableTreeNode Lightgray=new DefaultMutableTreeNode ( " Lightgray " ); gray.add (Lightgray ); DefaultMutableTreeNode Darkgray=new DefaultMutableTreeNode ( " Darkgray " ); gray.add (Darkgray ); DefaultMutableTreeNode Green=new DefaultMutableTreeNode ( " Green " ); color.add ( Green); root.add(color); JTree example5 = new JTree (root);//初始化jtree,把根节点color传入 jFrame.add(example5);//加入jframe面板 DragSource dragSource=DragSource.getDefaultDragSource();//创建拖拽环境 //设置拖拽组件,拖拽方式,拖拽数据准备传输 dragSource.createDefaultDragGestureRecognizer(example5, DnDConstants.ACTION_COPY_OR_MOVE , new MyDemoDragGestureListener()); new DropTarget(example5, new MyDemoDropTargetListener());//设置拖拽目的地的组件,上面所执行的操作 } } /** * 从被拖拽组件当中得到退拽数据 * @author Administrator * */ class MyDemoDragGestureListener implements DragGestureListener{ @Override public void dragGestureRecognized(DragGestureEvent dge) { // TODO Auto-generated method stub JTree tree = (JTree) dge.getComponent(); TreePath path = tree.getSelectionPath(); if (path != null) { DefaultMutableTreeNode selectTreeNode = (DefaultMutableTreeNode) path .getLastPathComponent(); //开始拖拽,保存拖拽数据,以及拖拽时候的鼠标样式 dge.startDrag(DragSource.DefaultCopyDrop, new MyDemoTransferable(selectTreeNode), new MyDemoDragSourceListener()); } } } /** * 暂时存放拖放数据的地方 * @author Administrator * */ class MyDemoTransferable implements Transferable { private DefaultMutableTreeNode treeNode; public MyDemoTransferable(DefaultMutableTreeNode treeNode){ this.treeNode=treeNode; } @Override public Object getTransferData(DataFlavor arg0) throws UnsupportedFlavorException, IOException { // TODO Auto-generated method stub return treeNode; } static DataFlavor flavors[] = { DataFlavor.stringFlavor }; @Override public DataFlavor[] getTransferDataFlavors() { // TODO Auto-generated method stub return flavors; } //是否支持指定数据flavors @Override public boolean isDataFlavorSupported(DataFlavor arg0) { // TODO Auto-generated method stub return true; } } /** * 设置拖拽鼠标样式的地方 * @author Administrator * */ class MyDemoDragSourceListener implements DragSourceListener{ @Override public void dragDropEnd(DragSourceDropEvent arg0) { // TODO Auto-generated method stub } @Override//进入拖拽 public void dragEnter(DragSourceDragEvent arg0) { // TODO Auto-generated method stub DragSourceContext context = arg0.getDragSourceContext(); System.out.println(context.getComponent()); context.setCursor(DragSource.DefaultMoveDrop); } @Override//离开拖拽 public void dragExit(DragSourceEvent arg0) { // TODO Auto-generated method stub DragSourceContext context = arg0.getDragSourceContext(); context.setCursor(DragSource.DefaultCopyNoDrop); } @Override public void dragOver(DragSourceDragEvent arg0) { // TODO Auto-generated method stub } @Override public void dropActionChanged(DragSourceDragEvent arg0) { // TODO Auto-generated method stub } } /** * 拖拽目的地所执行的操作 * @author Administrator * */ class MyDemoDropTargetListener implements DropTargetListener{ @Override public void dragEnter(DropTargetDragEvent dtde) { // TODO Auto-generated method stub } @Override public void dragExit(DropTargetEvent dte) { // TODO Auto-generated method stub } @Override//拖动过程中调用 public void dragOver(DropTargetDragEvent dtde) { // TODO Auto-generated method stub DropTarget target = (DropTarget) dtde.getSource(); JTree targetTree = (JTree) target.getComponent(); TreePath treePath=targetTree.getPathForLocation(dtde.getLocation().x, dtde.getLocation().y); if(treePath!=null){ targetTree.setSelectionPath(treePath); } } @Override//拖拽结束时候调用 public void drop(DropTargetDropEvent dtde) { // TODO Auto-generated method stub Transferable tr = dtde.getTransferable();// 使用该函数从Transferable对象中获取有用的数据 DefaultMutableTreeNode yuanNode=null;//拖过来的节点 try { if (tr.isDataFlavorSupported(DataFlavor.stringFlavor)) { yuanNode = (DefaultMutableTreeNode)tr.getTransferData(DataFlavor.stringFlavor);//从Transferable这里面得到 } } catch (IOException ex) { } catch (UnsupportedFlavorException ex) { } System.out.println("**********"+yuanNode.getUserObject()); String str = ""; try { if (tr.isDataFlavorSupported(DataFlavor.stringFlavor)) { str = tr.getTransferData(DataFlavor.stringFlavor).toString(); } } catch (IOException ex) { } catch (UnsupportedFlavorException ex) { } System.out.println(str); DropTarget target = (DropTarget) dtde.getSource(); JTree targetTree = (JTree) target.getComponent(); TreePath treePath=targetTree.getPathForLocation(dtde.getLocation().x, dtde.getLocation().y); TreePath t= new TreePath(yuanNode.getPath()); targetTree.setSelectionPath(t); targetTree.removeSelectionPath(t); DefaultMutableTreeNode mudidi=(DefaultMutableTreeNode)treePath.getLastPathComponent(); mudidi.add(yuanNode); targetTree.updateUI(); System.out.println("最后落脚的节点:"+treePath); System.out.println("*********end*************"); } @Override public void dropActionChanged(DropTargetDragEvent dtde) { // TODO Auto-generated method stub } }
DragSource dragSource=DragSource.getDefaultDragSource();//创建拖拽环境
//设置拖拽组件,拖拽方式,拖拽数据准备传输
dragSource.createDefaultDragGestureRecognizer(example5, DnDConstants.ACTION_COPY_OR_MOVE , new MyDemoDragGestureListener());
其中MyDemoDragGestureListener 里面有一个方法很重要:
//开始拖拽,保存拖拽数据,以及拖拽时候的鼠标样式
dge.startDrag(DragSource.DefaultCopyDrop, new MyDemoTransferable(selectTreeNode), new MyDemoDragSourceListener());
其中MyDemoTransferable 是传输被拖拽的数据。
MyDemoDragSourceListener是拖拽的时候鼠标的样式
//MyDemoDropTargetListener是目的地控件所做的动作
new DropTarget(example5, new MyDemoDropTargetListener());//设置拖拽目的地的组件,上面所执行的操作
要实现jtree的拖拽要实现4个接口即可:
DragGestureListener:准备要拖住数据其中startDrag方法很重要
Transferable://被拖住对象存放数据的地方接口
DragSourceListener:鼠标拖拽的样式接口,鼠标在目的地“进入,经过,离开”的动作监控
DropTargetListener:拖拽目的地所执行的操作,被拖拽对象在目的地“进入、经过、离开”的动作监控
这样就可以实现jtree的拖拽了,其实java的拖拽都是这样的,只要实现这几个接口任何空间都可以进行拖拽的。