当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Drag&Drop with swing

问题描述:

I need some help. Is it possible to simulate a drag & drop without registering a component?

E.g. I click the mousekey anywhere on the window and hold the mousekey down, at this moment, I want to create or simulate a DragSourceEvent programmatically with Java.

Is this possible?

Update:

Regarding Bob's reply, at least I got it, I can create a listener for the drag & drop:

DragSource dragSource = new DragSource();

DragGestureListener listener = new DragGestureListener() {

public void dragGestureRecognized(DragGestureEvent event) {

event.startDrag (null, strSel) ;

...

}

}

listener.dragGestureRecognized(new DragGestureEvent(

new DragGestureRecognizer(dragSource, component) {

}, DnDConstants.ACTION_COPY, new Point(0,0), events ));

but unfortunately i get this exception:

java.lang.IllegalArgumentException:

source actions at

java.awt.dnd.DragSourceContext.(DragSourceContext.java:169)

at

java.awt.dnd.DragSource.createDragSourceContext(DragSource.java:454)

at

java.awt.dnd.DragSource.startDrag(DragSource.java:293)

at

java.awt.dnd.DragSource.startDrag(DragSource.java:403)

at

java.awt.dnd.DragGestureEvent.startDrag(DragGestureEvent.java:203)

any suggestions?

网友答案:

The question you asked: I haven't tried it, but in theory you should be able to create the Event object and get a handle on the Swing Event Queue from one of the system classes. However without having a valid component, there may be problems when methods try to work with the event.

What you probably meant: Registering events for a standard window -- you should be able to set up drag and drop support for an empty JPanel or JFrame, but it'll take some hacking. Drag & Drop is a pain to work with at this level when not built in -- I suggest using something like an invisible component or something instead.

分享给朋友:
您可能感兴趣的文章:
随机阅读: