sun在java2中引入了一些新的方法來幫助實現拖拽功能,這些新的類在java.awt.dnd包中
實現一個D&D操作一般包括三個步驟:
首先實現一個拖拽源,這個拖拽源和相應的組件是關聯起來的
第二步實現一個拖拽目標,這個目標用來實現拖拽物的接收
第三步實現一個數據傳輸對象,該對象封裝拖動的數據
_____________________ _____________________
| | | |
| DragSource Component| |DropTarget Component|
|_____________________| |____________________|
| |
|____________Transferable Data_________________|
Transferable 接口實現出的對象能夠保證 DropTarget Component讀懂拖拽過來的對象中包含的信息
如果是在同一個虛擬機中實現拖拽的話,DragSource Component會傳遞一個引用給DropTarget Component
但是如果在不同的JVM中或者是在JVM和本地系統之間傳遞數據的話我們就必須實現一個Transferable對象來傳遞數據
Transferable中封裝的內容存放到DataFlavors,用戶可以通過訪問DataFlavors來獲取數據
1。創建可拖拽對象
一個對象那個如果想作為拖拽源的話,必須和五個對象建立聯系,這五個對象分別是:
* java.awt.dnd.DragSource
獲取DragSource的方法很簡單,直接調用DragSource.getDefaultDragSource();就可以得到DragSource對象
* java.awt.dnd.DragGestureRecognizer
DragGestureRecognizer類中實現了一些與平台無關的方法,我們如果想在自己的組件上實現拖拽的話只要調用createDefaultDragGestureRecognizer()方法就可以了
該方法接收三個參數,建立組件和拖拽動作之間的關系
* java.awt.dnd.DragGestureListener
當建立了組件和拖拽動作之間的聯系后,如果用戶執行了拖拽操作,組件將發送一個消息給DragGestureListener監聽器
DragGestureListener監聽器接下來會發送一個startDrag()消息給拖拽源對象,告訴組件應該執行拖拽的初始化操作了
拖拽源會產生一個DragSourceContext對象來監聽動作的狀態,這個監聽過程是通過監聽本地方法DragSourceContextPeer來實現的
* java.awt.datatransfer.Transferable
* java.awt.dnd.DragSourceListener
DragSourceListener接口負責當鼠標拖拽對象經過組件時的可視化處理, DragSourceListener接口的顯示結果只是暫時改變組件的外觀
同時他提供一個feedback,當用戶的拖拽操作完成之后會收到一個dragDropEnd的消息,我們可以在這個函數中執行相應的操作
再來回顧一下拖拽源的建立過程
首先、 DragGestureRecognizer 確認一個拖拽操作,同時告知 DragGestureListener.
其次、 Assuming the actions and/or flavors are OK, DragGestureListener asks DragSource to startDrag().
第三、 DragSource creates a DragSourceContext and a DragSourceContextPeer. The DragSourceContext adds itself as a DragSourceListener to the DragSourceContextPeer.
第四、 DragSourceContextPeer receives state notifications (component entered/exited/is over) from the native system and delegates them to the DragSourceContext.
第五、 The DragSourceContext notifies the DragSourceListener, which provides drag over feedback (if the DropTargetListener accepts the action). Typical feedback includes asking the DragSourceContext to change the cursor.
最后、 When the drop is complete, the DragSourceListener receives a dragDropEnd notification message
2。創建droppable Component
創建一個 droppable Component必須和下面兩個對象發生關聯
* java.awt.dnd.DropTarget
DropTarget構造函數使DropTarget 和 DropTargetListener objects發生關聯
Droptarget對象提供 setComponent 和addDropTargetListener 兩個方法
* java.awt.dnd.DropTargetListener
The DropTargetListener needs an association with the Component so that the Component can notify the DropTargetListener to display "drag under" effects during the operation. This listener, which can be conveniently created as an inner class, transfers the data when the drop occurs. Warning: The Component itself shouldn't be the listener, since this implies its availability for use as some other Component's listener.
1 public class DragAndDrop extends JFrame { 2 private static final long serialVersionUID = 1L; 3 JScrollPane jScrollPane1 = new JScrollPane(); 4 JTextField jtf = new JTextField(); 5 6 public DragAndDrop() { 7 getContentPane().setLayout(new BorderLayout()); 8 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 9 jtf.setBackground(Color.yellow); 10 jtf.setSize(100, 80); 11 JPanel panel1 = new JPanel(new BorderLayout()); 12 panel1.add(jtf, BorderLayout.CENTER); 13 JTree jtr = new JTree(); 14 jScrollPane1.getViewport().add(jtr); 15 add(panel1, BorderLayout.SOUTH); 16 add(jScrollPane1, BorderLayout.CENTER); 17 18 DragSource dragSource = DragSource.getDefaultDragSource(); // 創建拖拽源 19 dragSource.createDefaultDragGestureRecognizer(jtr, 20 DnDConstants.ACTION_COPY_OR_MOVE, new MyDragGestureListener()); // 建立拖拽源和事件的聯系 21 new DropTarget(jtf, new MyTargetListener()); 22 23 } 24 25 public static void main(String[] args) { 26 DragAndDrop dad = new DragAndDrop(); 27 dad.setTitle("拖拽演示"); 28 dad.setSize(400, 300); 29 dad.setVisible(true); 30 31 } 32 } 33 34 class MyDragGestureListener implements DragGestureListener { 35 public void dragGestureRecognized(DragGestureEvent dge) { 36 // 將數據存儲到Transferable中,然后通知組件開始調用startDrag()初始化 37 JTree tree = (JTree) dge.getComponent(); 38 TreePath path = tree.getSelectionPath(); 39 if (path != null) { 40 DefaultMutableTreeNode selection = (DefaultMutableTreeNode) path 41 .getLastPathComponent(); 42 MyTransferable dragAndDropTransferable = new MyTransferable( 43 selection); 44 dge.startDrag(DragSource.DefaultCopyDrop, dragAndDropTransferable, 45 new MySourceListener()); 46 } 47 } 48 49 } 50 51 class MyTransferable implements Transferable { 52 private DefaultMutableTreeNode treeNode; 53 54 MyTransferable(DefaultMutableTreeNode treeNode) { 55 this.treeNode = treeNode; 56 } 57 58 static DataFlavor flavors[] = { DataFlavor.stringFlavor }; 59 60 public DataFlavor[] getTransferDataFlavors() { 61 return flavors; 62 } 63 64 public boolean isDataFlavorSupported(DataFlavor flavor) { 65 // if (treeNode.getChildCount() == 0) { 66 // return true; 67 // } 68 return true; 69 } 70 71 public Object getTransferData(DataFlavor flavor) 72 throws UnsupportedFlavorException, IOException { 73 74 return treeNode; 75 76 } 77 78 } 79 80 class MySourceListener implements DragSourceListener { 81 public void dragDropEnd(DragSourceDropEvent dragSourceDropEvent) { 82 if (dragSourceDropEvent.getDropSuccess()) { 83 // 拖拽動作結束的時候打印出移動節點的字符串 84 int dropAction = dragSourceDropEvent.getDropAction(); 85 if (dropAction == DnDConstants.ACTION_MOVE) { 86 System.out.println("MOVE: remove node"); 87 } 88 } 89 } 90 91 public void dragEnter(DragSourceDragEvent dragSourceDragEvent) { 92 DragSourceContext context = dragSourceDragEvent.getDragSourceContext(); 93 int dropAction = dragSourceDragEvent.getDropAction(); 94 if ((dropAction & DnDConstants.ACTION_COPY) != 0) { 95 context.setCursor(DragSource.DefaultCopyDrop); 96 } else if ((dropAction & DnDConstants.ACTION_MOVE) != 0) { 97 context.setCursor(DragSource.DefaultMoveDrop); 98 } else { 99 context.setCursor(DragSource.DefaultCopyNoDrop); 100 } 101 } 102 103 public void dragExit(DragSourceEvent dragSourceEvent) { 104 } 105 106 public void dragOver(DragSourceDragEvent dragSourceDragEvent) { 107 } 108 109 public void dropActionChanged(DragSourceDragEvent dragSourceDragEvent) { 110 } 111 } 112 113 class MyTargetListener implements DropTargetListener { 114 public void dragEnter(DropTargetDragEvent dtde) { 115 116 } 117 118 public void dragOver(DropTargetDragEvent dtde) { 119 } 120 121 public void dropActionChanged(DropTargetDragEvent dtde) { 122 } 123 124 public void dragExit(DropTargetEvent dte) { 125 } 126 127 public void drop(DropTargetDropEvent dtde) { 128 Transferable tr = dtde.getTransferable();// 使用該函數從Transferable對象中獲取有用的數據 129 String str = ""; 130 try { 131 if (tr.isDataFlavorSupported(DataFlavor.stringFlavor)) { 132 str = tr.getTransferData(DataFlavor.stringFlavor).toString(); 133 } 134 } catch (IOException ex) { 135 } catch (UnsupportedFlavorException ex) { 136 } 137 System.out.println(str); 138 DropTarget target = (DropTarget) dtde.getSource(); 139 JTextField filed = (JTextField) target.getComponent(); 140 if (str != null && str != "") { 141 filed.setText(filed.getText() + str); 142 } 143 } 144 }