开发者

Java Swing Drag and Drop with popup dialog

开发者 https://www.devze.com 2023-03-03 03:32 出处:网络
I want the following behavior in a Java Swing application: User drags and drops an icon. User is prompted with a modal dialog if other icons should also be operated upon.

I want the following behavior in a Java Swing application:

  1. User drags and drops an icon.
  2. User is prompted with a modal dialog if other icons should also be operated upon.
  3. After the user chooses YES, NO, or CANCEL (or closes the dialog), Java begins operating on (copying/moving) the icons (or on cancel/close does nothing).

Within TransferHandler.canImport() I have visibility to the drop target but not to the icon.

Within TransferHandler.exportDone() I have visibility to the icon but not to the drop target.

Within TransferHandler.importData() I hav开发者_JAVA技巧e visibility to both, but by then it's too late since if I'm performing a TransferHandler.MOVE then TransferHandler.exportDone() has already removed the icon.

Is the behavior I mentioned above possible using Java Swing? If so, what version of the JDK/JRE do I need to use and how do I implement it?


If you want to be able to possibly drag-and-drop items, as well as move/copy them programatically, you need to make sure you have a proper model set up which backs the view. This means you shouldn't be coding your business logic directly into the GUI code. The model should be simple and independent, focusing on the actual task of managing the data. In your case, you it sounds like you just need to hold some collections of objects and provide methods which move/copy objects from one collection to the other. The code which does this is called the controller (MVC - pardon me if you are already familiar with this pattern). The controller will then update the GUI to reflect the changes. Similarly, your drag-and-drop will trigger a controller action which performs a similar action on the model.

0

精彩评论

暂无评论...
验证码 换一张
取 消