Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drag&Drop with swing

Tags:

java

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?

like image 992
user217333 Avatar asked Nov 14 '22 14:11

user217333


1 Answers

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.

like image 122
BobMcGee Avatar answered Dec 23 '22 10:12

BobMcGee