在JAVA中使用拖拽功能
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.
下面的例子演示了一个从树中拖拽一个节点到文本域中
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(); //创建拖拽源
dragSource.createDefaultDragGestureRecognizer(jtr,
DnDConstants.ACTION_COPY_OR_MOVE,
new DragAndDropDragGestureListener()); //建立拖拽源和事件的联系
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) {
//将数据存储到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 (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");
}
}
}