java - 拖放 : Trying to understand DropDemo tutorial

标签 java swing drag-and-drop

我现在正在研究拖放,我在 Oracle 网站上发现了一个关于自定义 TransferHandlers 等的有趣教程。无论如何,我一直在关注自定义处理程序的源代码在他们提供的 DropDemo 程序中,这让我发疯。

令我困惑的是,似乎有一大堆方法没有被调用:importData()、importString() 和exportDone()。它们似乎都提供重要功能,并且都必须在某个时刻触发,但我似乎看不到这些方法在程序中的何处被调用。

我最终试图了解 DropDemo 如何提供通过拖放重新排序 JList 的能力,因为到目前为止我还无法复制这一点。然而,这已经变成了我更大的问题。

最佳答案

Transferable#importData(TransferHandler.TransferSupport)

Causes a transfer to occur from a clipboard or a drag and drop operation. The Transferable to be imported and the component to transfer to are contained within the TransferSupport.

While the drag and drop implementation calls canImport to determine the suitability of a transfer before calling this method, the implementation of paste does not. As such, it cannot be assumed that the transfer is acceptable upon a call to this method for paste. It is recommended that canImport be explicitly called to cover this case.

Note: The TransferSupport object passed to this method is only valid for the duration of the method call. It is undefined what values it may contain after this method returns.

基本上,当与 Transferable 关联的组件检测到“导入”事件时,就会调用此函数。这是 Transferable 根据其要求从 TransferHandler.TransferSupport 物理导入数据的机会

Transferable#exportDone

Invoked after data has been exported. This method should remove the data that was transferred if the action was MOVE.

This method is implemented to do nothing since MOVE is not a supported action of this implementation (getSourceActions does not include MOVE).

这是在启动导出过程的Transferable实例上调用的,告诉它传输的接收者采取了什么操作,以便它可以采取适当的操作

需要注意的是,并非所有方法都必须调用,例如,如果您正在导出数据,则不会通知您 importData 方法。同样,如果传输从未成功完成,则可能不会调用 exportDone

这些方法由核心 D'n'D 子系统触发,Transferable API 位于其之上,并响应较低级别的事件而完成。您实际上并不需要“知道”它们如何被调用,而是需要知道它们被调用“时”要做什么。这有点像事件驱动程序的“黑魔法”。

I'm ultimately trying to understand how DropDemo offers the ability to re-order a JList through drag and drop, since I have so far been unable to replicate this. However, this has morphed into my larger question. So thanks in advance.

For example

关于java - 拖放 : Trying to understand DropDemo tutorial,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29787622/

相关文章:

java - 如何在 Java 中从 Excel 中的列名中读取值

java - 如何在android中检查拖放是否为空

c++ - 使标题图标成为资源管理器窗口中的拖动源

Java 方法按值传递问题

java - java中的try-catch-finally

java - 删除 jtabbedpane 的最后一个选项卡

java - JCheckbox 未显示

java - 父面板未正确重绘

重新排序树时超出 ExtJS 最大调用堆栈大小

java - 随机生成对象