下面列出了java.awt.dnd.DnDConstants#ACTION_COPY_OR_MOVE 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/** Utility method.
* @return true if given node supports given action,
* false otherwise.
*/
static boolean checkNodeForAction(Node node, int dragAction) {
if (
node.canCut() &&
((dragAction == DnDConstants.ACTION_MOVE) || (dragAction == DnDConstants.ACTION_COPY_OR_MOVE))
) {
return true;
}
if (
node.canCopy() &&
((dragAction == DnDConstants.ACTION_COPY) || (dragAction == DnDConstants.ACTION_COPY_OR_MOVE) ||
(dragAction == DnDConstants.ACTION_LINK) || (dragAction == DnDConstants.ACTION_REFERENCE))
) {
return true;
}
// hmmm, conditions not satisfied..
return false;
}
public static void main(String[] args) throws Exception {
Frame sourceFrame = createFrame("Source Frame", 0, 0);
Frame targetFrame = createFrame("Target Frame", 250, 250);
DragSource defaultDragSource
= DragSource.getDefaultDragSource();
defaultDragSource.createDefaultDragGestureRecognizer(sourceFrame,
DnDConstants.ACTION_COPY_OR_MOVE,
new TestDragGestureListener());
new DropTarget(targetFrame, DnDConstants.ACTION_COPY_OR_MOVE,
new TestDropTargetListener(targetFrame));
Robot robot = new Robot();
robot.setAutoDelay(50);
sourceFrame.toFront();
robot.waitForIdle();
Point point = getCenterPoint(sourceFrame);
robot.mouseMove(point.x, point.y);
robot.waitForIdle();
mouseDragAndDrop(robot, point, getCenterPoint(targetFrame));
long time = System.currentTimeMillis() + 200;
while (!passed) {
if (time < System.currentTimeMillis()) {
sourceFrame.dispose();
targetFrame.dispose();
throw new RuntimeException("Mouse clicked event is lost!");
}
Thread.sleep(10);
}
sourceFrame.dispose();
targetFrame.dispose();
}
public static void main(String[] args) throws Exception {
Frame sourceFrame = createFrame("Source Frame", 0, 0);
Frame targetFrame = createFrame("Target Frame", 250, 250);
DragSource defaultDragSource
= DragSource.getDefaultDragSource();
defaultDragSource.createDefaultDragGestureRecognizer(sourceFrame,
DnDConstants.ACTION_COPY_OR_MOVE,
new TestDragGestureListener());
new DropTarget(targetFrame, DnDConstants.ACTION_COPY_OR_MOVE,
new TestDropTargetListener(targetFrame));
Robot robot = new Robot();
robot.setAutoDelay(50);
sourceFrame.toFront();
robot.waitForIdle();
Point point = getCenterPoint(sourceFrame);
robot.mouseMove(point.x, point.y);
robot.waitForIdle();
mouseDragAndDrop(robot, point, getCenterPoint(targetFrame));
long time = System.currentTimeMillis() + 200;
while (!passed) {
if (time < System.currentTimeMillis()) {
sourceFrame.dispose();
targetFrame.dispose();
throw new RuntimeException("Mouse clicked event is lost!");
}
Thread.sleep(10);
}
sourceFrame.dispose();
targetFrame.dispose();
}
AutoScrollingTreeDropTarget(JTree aTree, DropTargetListener listener) {
super(aTree, DnDConstants.ACTION_COPY_OR_MOVE, listener);
this.viewport = (JViewport) SwingUtilities.getAncestorOfClass(
JViewport.class, aTree);
this.scrollUnits = Math.max(aTree.getRowHeight(), 16);
this.tree = aTree;
}
public static void main(String[] args) throws Exception {
Frame sourceFrame = createFrame("Source Frame", 0, 0);
Frame targetFrame = createFrame("Target Frame", 250, 250);
DragSource defaultDragSource
= DragSource.getDefaultDragSource();
defaultDragSource.createDefaultDragGestureRecognizer(sourceFrame,
DnDConstants.ACTION_COPY_OR_MOVE,
new TestDragGestureListener());
new DropTarget(targetFrame, DnDConstants.ACTION_COPY_OR_MOVE,
new TestDropTargetListener(targetFrame));
Robot robot = new Robot();
robot.setAutoDelay(50);
sourceFrame.toFront();
robot.waitForIdle();
Point point = getCenterPoint(sourceFrame);
robot.mouseMove(point.x, point.y);
robot.waitForIdle();
mouseDragAndDrop(robot, point, getCenterPoint(targetFrame));
long time = System.currentTimeMillis() + 200;
while (!passed) {
if (time < System.currentTimeMillis()) {
sourceFrame.dispose();
targetFrame.dispose();
throw new RuntimeException("Mouse clicked event is lost!");
}
Thread.sleep(10);
}
sourceFrame.dispose();
targetFrame.dispose();
}
public static void drag()//定义的拖拽方法
{
//panel表示要接受拖拽的控件
new DropTarget(JlPath, DnDConstants.ACTION_COPY_OR_MOVE, new DropTargetAdapter() {
public void drop(DropTargetDropEvent dtde)//重写适配器的drop方法
{
try {
if (dtde.isDataFlavorSupported(DataFlavor.javaFileListFlavor))//如果拖入的文件格式受支持
{
dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);//接收拖拽来的数据
List<File> list = (List<File>) (dtde.getTransferable().getTransferData(DataFlavor.javaFileListFlavor));
String temp = "";
for (File file : list) {
temp = file.getAbsolutePath();
JlPath.setText(temp);
break;
}
//JOptionPane.showMessageDialog(null, temp);
dtde.dropComplete(true);//指示拖拽操作已完成
} else {
dtde.rejectDrop();//否则拒绝拖拽来的数据
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public int getAllowedDropActions(Transferable t) {
if (t != null && t.isDataFlavorSupported(new DataFlavor(Watch.class, null))) {
return DnDConstants.ACTION_COPY_OR_MOVE;
} else {
return DnDConstants.ACTION_COPY;
}
}
public static void main(String[] args) throws Exception {
Frame sourceFrame = createFrame("Source Frame", 0, 0);
Frame targetFrame = createFrame("Target Frame", 250, 250);
DragSource defaultDragSource
= DragSource.getDefaultDragSource();
defaultDragSource.createDefaultDragGestureRecognizer(sourceFrame,
DnDConstants.ACTION_COPY_OR_MOVE,
new TestDragGestureListener());
new DropTarget(targetFrame, DnDConstants.ACTION_COPY_OR_MOVE,
new TestDropTargetListener(targetFrame));
Robot robot = new Robot();
robot.setAutoDelay(50);
sourceFrame.toFront();
robot.waitForIdle();
Point point = getCenterPoint(sourceFrame);
robot.mouseMove(point.x, point.y);
robot.waitForIdle();
mouseDragAndDrop(robot, point, getCenterPoint(targetFrame));
long time = System.currentTimeMillis() + 200;
while (!passed) {
if (time < System.currentTimeMillis()) {
sourceFrame.dispose();
targetFrame.dispose();
throw new RuntimeException("Mouse clicked event is lost!");
}
Thread.sleep(10);
}
sourceFrame.dispose();
targetFrame.dispose();
}
public static void main(String[] args) throws Exception {
Frame sourceFrame = createFrame("Source Frame", 0, 0);
Frame targetFrame = createFrame("Target Frame", 250, 250);
DragSource defaultDragSource
= DragSource.getDefaultDragSource();
defaultDragSource.createDefaultDragGestureRecognizer(sourceFrame,
DnDConstants.ACTION_COPY_OR_MOVE,
new TestDragGestureListener());
new DropTarget(targetFrame, DnDConstants.ACTION_COPY_OR_MOVE,
new TestDropTargetListener(targetFrame));
Robot robot = new Robot();
robot.setAutoDelay(50);
sourceFrame.toFront();
robot.waitForIdle();
Point point = getCenterPoint(sourceFrame);
robot.mouseMove(point.x, point.y);
robot.waitForIdle();
mouseDragAndDrop(robot, point, getCenterPoint(targetFrame));
long time = System.currentTimeMillis() + 1000;
while (!passed) {
if (time < System.currentTimeMillis()) {
sourceFrame.dispose();
targetFrame.dispose();
throw new RuntimeException("Mouse clicked event is lost!");
}
Thread.sleep(10);
}
sourceFrame.dispose();
targetFrame.dispose();
}
/**
* @param actions set the current actions
*/
public synchronized void setTargetActions(int actions) {
currentA = actions &
(DnDConstants.ACTION_COPY_OR_MOVE | DnDConstants.ACTION_LINK);
}
@Override
public int getSupportedDragActions() {
return DnDConstants.ACTION_COPY_OR_MOVE;
}
@Override
public int getSupportedDragActions() {
return DnDConstants.ACTION_COPY_OR_MOVE;
}
/**
* @param actions set the current actions
*/
public synchronized void setTargetActions(int actions) {
currentA = actions &
(DnDConstants.ACTION_COPY_OR_MOVE | DnDConstants.ACTION_LINK);
}
/**
* @param actions set the current actions
*/
public synchronized void setTargetActions(int actions) {
currentA = actions &
(DnDConstants.ACTION_COPY_OR_MOVE | DnDConstants.ACTION_LINK);
}
/**
* @param actions set the current actions
*/
public synchronized void setTargetActions(int actions) {
currentA = actions &
(DnDConstants.ACTION_COPY_OR_MOVE | DnDConstants.ACTION_LINK);
}
public int getAllowedDragActions() {
return DnDConstants.ACTION_COPY_OR_MOVE;
}
/**
* @param actions set the current actions
*/
public synchronized void setTargetActions(int actions) {
currentA = actions &
(DnDConstants.ACTION_COPY_OR_MOVE | DnDConstants.ACTION_LINK);
}
/**
* @param actions set the current actions
*/
public synchronized void setTargetActions(int actions) {
currentA = actions &
(DnDConstants.ACTION_COPY_OR_MOVE | DnDConstants.ACTION_LINK);
}
/**
* @param actions set the current actions
*/
public synchronized void setTargetActions(int actions) {
currentA = actions &
(DnDConstants.ACTION_COPY_OR_MOVE | DnDConstants.ACTION_LINK);
}
/**
* @param actions set the current actions
*/
public synchronized void setTargetActions(int actions) {
currentA = actions &
(DnDConstants.ACTION_COPY_OR_MOVE | DnDConstants.ACTION_LINK);
}