• Post Reply Bookmark Topic Watch Topic
  • New Topic

Preventing multiple drag and drop  RSS feed

 
T H Lim
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am writing a drop zone application where I will load the information of the file dropped in. The information will be extracted from the file and stored across the network so there will be a couple of seconds between each drop. What I want is to disable the drop zone and show a message "Sending..." during that period of time. So far what I managed to do is prevent the application from accepting new drop-ins but the mouse cursor icon doesn't reflect that and it basically hung my application interface because I was sitting on the AWT-thread during the upload and processing. I am using glass pane to show the message "Sending..." Any idea how I can improve this?

Thanks

/lim/

 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
setCursor(Cursor.WAIT_CURSOR)?
 
T H Lim
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the mouse cursor changed doesn't stop the user from dragging more files into the drop zone.
 
Tarun Bolla
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ... regarding the cursor you can use COMPONENT.setCursor(DragSource.DefaultMoveNoDrop);

And regarding the threading of you background process ... you want to take a look at...SwingUtilities.invokeLater(<RUNNABLE INSTANCE>) which runs your logic in an anonymous inner class of type Runnable

EDIT : And regarding the stopping of any more files being dropped, you can just discard the files that are dropped until the process in background thread is finished. You can do this using a global variable. In the background thread (RUNNABLE INSTANCE), set cursor and set a global variable (like isWorkInProgress = true). At this point you can discard the files that are being dropped. After process is finished reset the global variable, cursor.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First you wrote:
TH Lim wrote:So far what I managed to do is prevent the application from accepting new drop-ins but the mouse cursor icon doesn't reflect that

Then you reply to my suggestion that addresses that issue:
TH Lim wrote:the mouse cursor changed doesn't stop the user from dragging more files into the drop zone.

I thought you had already 'managed' that?

If you want to provide feedback to the user, you may find Rob Camick's Disabled Panel or Disabled Glass Pane useful.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tarun Bolla wrote:And regarding the threading of you background process ... you want to take a look at...SwingUtilities.invokeLater

Better still, use a SwingWorker. That's what the class is designed for.
 
T H Lim
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I solved my problem the work in progress flag. What I did was, I spawn a new Thread. Within this Thread, I made a remote request. Once it has completed, I updated the Swing components using SwingUtilities.invokeLater(...) within the same Thread. Finally, reset the work in progress flag to false. DropTargetAdapter's dragEnter(...) and drop(...) will check work in progress flag and reject any further drop-in outright until the 1st task is completed.

I tried using SwingWorker but it didn't seem to solve my problem. It seems to me, SwingWorker still works within the single AWT thread. This means that when a drop in event happened and the AWT thread is busy, the event will be queued. Once the AWT thread is freed, it will pick up whatever in the queue. It will add those files which i dragged into drop-in area during the "drop-in disabled" period. As compared to the earlier solution where the AWT thread is free-ed, accepted the drop-in event and rejected it.

I don't understand SwingWorker well and I could have probably made a mistake. Any advise would be a great help.

Thank you guys for chipping in.
 
T H Lim
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as the cursor concerned, I used the system cursor.
 
Rob Spoor
Sheriff
Posts: 20903
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
SwingWorker uses its own thread, except for the process and done methods; those are executed in the EDT.
 
T H Lim
Greenhorn
Posts: 28
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I relooked my code with SwingWorker. Now it works as with my custom Thread handling. Thanks
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!