演示 - ChooseDropAction

原文: https://docs.oracle.com/javase/tutorial/uiswing/dnd/dropactiondemo.html

以下演示ChooseDropActionDemo包含三个列表。正如您在屏幕截图中看到的那样,左侧的列表标记为“从此处拖动”,是拖动源。此列表支持移动和复制,但它不实现导入 - 因此您不能放入它。

在右侧有两个列表作为放置目标。标有“Drop to COPY here”的顶部列表仅允许将数据复制到其中。标有“Drop to MOVE here”的底部列表只允许将数据移动到它。源列表仅允许从中拖动数据。

A snapshot of the ChooseDropActionDemo demo.


Try this:

  1. 单击启动按钮以使用 Java™Web Start下载 JDK 7 或更高版本)运行ChooseDropActionDemo。或者,要自己编译并运行示例,请参考示例索引Launches the ChooseDropActionDemo example

  2. 在源列表中选择一个项目并拖动到上部目标列表。当您在目标上拖动时,请注意,即使您没有按住 Control 键表示您想要复制操作,也会显示复制 - 拖放鼠标光标。 (请注意,除非您按 Option 键,否则复制光标不会出现在 Macintosh 平台上。)

  3. 放下物品。它被插入到目标列表中,但不会从源中删除 - 根据需要。
  4. 再次从源列表中拖动,但这次进入较低的目标列表。放下物品。它将插入目标列表并从源列表中删除。
  5. 在源列表中选择另一个项目,并在按 Control 键指示 COPY 操作的首选项时,将项目拖动到下一个目标列表。
  6. 将项目放入列表中。未插入项目 - 拒绝丢弃。传输处理器的canImport方法被编码为拒绝 COPY 操作,但它可以被实现为返回 true,在这种情况下,用户操作将占优势并且将发生复制。

正如您可能猜到的, ChooseDropActionDemo.java示例包含两个TransferHandler实现:

  1. /**
  2. * The FromTransferHandler allows dragging from the list and
  3. * supports both copy and move actions. This transfer handler
  4. * does not support import.
  5. */
  6. class FromTransferHandler extends TransferHandler {
  7. public int getSourceActions(JComponent comp) {
  8. return COPY_OR_MOVE;
  9. }
  10. private int index = 0;
  11. public Transferable createTransferable(JComponent comp) {
  12. index = dragFrom.getSelectedIndex();
  13. if (index < 0 || index >= from.getSize()) {
  14. return null;
  15. }
  16. return new StringSelection((String)dragFrom.getSelectedValue());
  17. }
  18. public void exportDone(JComponent comp, Transferable trans, int action) {
  19. if (action != MOVE) {
  20. return;
  21. }
  22. from.removeElementAt(index);
  23. }
  24. }
  25. /**
  26. * The ToTransferHandler has a constructor that specifies whether the
  27. * instance will support only the copy action or the move action.
  28. * This transfer handler does not support export.
  29. */
  30. class ToTransferHandler extends TransferHandler {
  31. int action;
  32. public ToTransferHandler(int action) {
  33. this.action = action;
  34. }
  35. public boolean canImport(TransferHandler.TransferSupport support) {
  36. // for the demo, we will only support drops (not clipboard paste)
  37. if (!support.isDrop()) {
  38. return false;
  39. }
  40. // we only import Strings
  41. if (!support.isDataFlavorSupported(DataFlavor.stringFlavor)) {
  42. return false;
  43. }
  44. // check if the source actions contain the desired action -
  45. // either copy or move, depending on what was specified when
  46. // this instance was created
  47. boolean actionSupported = (action & support.getSourceDropActions()) == action;
  48. if (actionSupported) {
  49. support.setDropAction(action);
  50. return true;
  51. }
  52. // the desired action is not supported, so reject the transfer
  53. return false;
  54. }
  55. public boolean importData(TransferHandler.TransferSupport support) {
  56. // if we cannot handle the import, say so
  57. if (!canImport(support)) {
  58. return false;
  59. }
  60. // fetch the drop location
  61. JList.DropLocation dl = (JList.DropLocation)support.getDropLocation();
  62. int index = dl.getIndex();
  63. // fetch the data and bail if this fails
  64. String data;
  65. try {
  66. data = (String)support.getTransferable().getTransferData(DataFlavor.stringFlavor);
  67. } catch (UnsupportedFlavorException e) {
  68. return false;
  69. } catch (java.io.IOException e) {
  70. return false;
  71. }
  72. JList list = (JList)support.getComponent();
  73. DefaultListModel model = (DefaultListModel)list.getModel();
  74. model.insertElementAt(data, index);
  75. Rectangle rect = list.getCellBounds(index, index);
  76. list.scrollRectToVisible(rect);
  77. list.setSelectedIndex(index);
  78. list.requestFocusInWindow();
  79. return true;
  80. }
  81. }

附加到源列表的FromTransferHandler允许从列表中拖动并支持复制和移动操作。如果您尝试删除此列表,则丢弃将被拒绝,因为FromTransferHandler尚未实现canImportimportData方法。

[COG0]附加到仅移动和仅复制目标列表,包含一个构造器,指定目标列表是仅允许复制还是仅移动。支持复制操作的实例附加到仅复制列表,支持移动操作的实例附加到仅移动列表。

您可能也对顶级 Drop 示例感兴趣,该示例还说明了选择放置操作。

接下来我们看一下显示放置位置。