在JAVA中使用拖拽功能
sun在java2中引入了一些新的方法來幫助實(shí)現(xiàn)拖拽功能,這些新的類在java.awt.dnd包中
實(shí)現(xiàn)一個(gè)D&D操作一般包括三個(gè)步驟:
首先實(shí)現(xiàn)一個(gè)拖拽源,這個(gè)拖拽源和相應(yīng)的組件是關(guān)聯(lián)起來的
第二步實(shí)現(xiàn)一個(gè)拖拽目標(biāo),這個(gè)目標(biāo)用來實(shí)現(xiàn)拖拽物的接收
第三步實(shí)現(xiàn)一個(gè)數(shù)據(jù)傳輸對象,該對象封裝拖動(dòng)的數(shù)據(jù)
_____________________ _____________________
| | | |
| DragSource Component| |DropTarget Component|
|_____________________| |____________________|
| |
|____________Transferable Data_________________|
Transferable 接口實(shí)現(xiàn)出的對象能夠保證 DropTarget Component讀懂拖拽過來的對象中包含的信息
如果是在同一個(gè)虛擬機(jī)中實(shí)現(xiàn)拖拽的話,DragSource Component會傳遞一個(gè)引用給DropTarget Component
但是如果在不同的JVM中或者是在JVM和本地系統(tǒng)之間傳遞數(shù)據(jù)的話我們就必須實(shí)現(xiàn)一個(gè)Transferable對象來傳遞數(shù)據(jù)
Transferable中封裝的內(nèi)容存放到DataFlavors,用戶可以通過訪問DataFlavors來獲取數(shù)據(jù)
1。創(chuàng)建可拖拽對象
一個(gè)對象那個(gè)如果想作為拖拽源的話,必須和五個(gè)對象建立練習(xí),這五個(gè)對象分別是:
* java.awt.dnd.DragSource
獲取DragSource的方法很簡單,直接調(diào)用DragSource.getDefaultDragSource();就可以得到DragSource對象
* java.awt.dnd.DragGestureRecognizer
DragGestureRecognizer類中實(shí)現(xiàn)了一些與平臺無關(guān)的方法,我們?nèi)绻朐谧约旱慕M件上實(shí)現(xiàn)拖拽的話只要調(diào)用createDefaultDragGestureRecognizer()方法就可以了
該方法接收三個(gè)參數(shù),建立組件和拖拽動(dòng)作之間的關(guān)系
* java.awt.dnd.DragGestureListener
當(dāng)建立了組件和拖拽動(dòng)作之間的聯(lián)系后,如果用戶執(zhí)行了拖拽操作,組件將發(fā)送一個(gè)消息給DragGestureListener監(jiān)聽器
DragGestureListener監(jiān)聽器接下來會發(fā)送一個(gè)startDrag()消息給拖拽源對象,告訴組件應(yīng)該執(zhí)行拖拽的初始化操作了
拖拽源會產(chǎn)生一個(gè)DragSourceContext對象來監(jiān)聽動(dòng)作的狀態(tài),這個(gè)監(jiān)聽過程是通過監(jiān)聽本地方法DragSourceContextPeer來實(shí)現(xiàn)的
* java.awt.datatransfer.Transferable
* java.awt.dnd.DragSourceListener
DragSourceListener接口負(fù)責(zé)當(dāng)鼠標(biāo)拖拽對象經(jīng)過組件時(shí)的可視化處理, DragSourceListener接口的顯示結(jié)果只是暫時(shí)改變組件的外觀
同時(shí)他提供一個(gè)feedback,當(dāng)用戶的拖拽操作完成之后會收到一個(gè)dragDropEnd的消息,我們可以在這個(gè)函數(shù)中執(zhí)行相應(yīng)的操作
再來回顧一下拖拽源的建立過程
首先、 DragGestureRecognizer 確認(rèn)一個(gè)拖拽操作,同時(shí)告知 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。創(chuàng)建droppable Component
創(chuàng)建一個(gè) droppable Component必須和下面兩個(gè)對象發(fā)生關(guān)聯(lián)
* java.awt.dnd.DropTarget
DropTarget構(gòu)造函數(shù)使DropTarget 和 DropTargetListener objects發(fā)生關(guān)聯(lián)
Droptarget對象提供 setComponent 和addDropTargetListener 兩個(gè)方法
* 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.
下面的例子演示了一個(gè)從樹中拖拽一個(gè)節(jié)點(diǎn)到文本域中
package appletandservlet;
import java.awt.*;
import javax.swing.*;
import com.borland.jbcl.layout.XYLayout;
import com.borland.jbcl.layout.*;
import java.awt.dnd.*;
import java.awt.datatransfer.*;
import java.io.*;
import javax.swing.tree.*;
public class DragAndDrop extends JFrame {
XYLayout xYLayout1 = new XYLayout();
JScrollPane jScrollPane1 = new JScrollPane();
JTextArea jTextArea1 = new JTextArea();
public DragAndDrop() {
try {
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
getContentPane().setLayout(xYLayout1);
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,
new XYConstraints(238, 42, 140, 248));
this.getContentPane().add(jScrollPane1,
new XYConstraints(16, 42, 217, 249));
DragSource dragSource = DragSource.getDefaultDragSource(); //創(chuàng)建拖拽源
dragSource.createDefaultDragGestureRecognizer(jtr,
DnDConstants.ACTION_COPY_OR_MOVE,
new DragAndDropDragGestureListener()); //建立拖拽源和事件的聯(lián)系
DropTarget dropTarget = 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) {
//將數(shù)據(jù)存儲到Transferable中,然后通知組件開始調(diào)用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()) {
//拖拽動(dòng)作結(jié)束的時(shí)候打印出移動(dòng)節(jié)點(diǎn)的字符串
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();//使用該函數(shù)從Transferable對象中獲取有用的數(shù)據(jù)
String s="";
try {
if(tr.isDataFlavorSupported(DataFlavor.stringFlavor)){
s = tr.getTransferData(DataFlavor.stringFlavor).toString();
}
} catch (IOException ex) {
} catch (UnsupportedFlavorException ex) {
}
System.out.println(s);
DropTarget c=(DropTarget)dtde.getSource();
JTextArea d=(JTextArea)c.getComponent();
if(s!=null&&s!=""){
d.append(s + "\n");
}
}
}