dropTarget

fun SwingModifier.dropTarget(acceptedActions: Int, onDrop: (transferable: Transferable) -> Boolean, canImport: (flavors: List<DataFlavor>) -> Boolean = { true }): SwingModifier

Makes the component a drop TARGET that imports a dropped Transferable.

acceptedActions declares which operations the target accepts; a drop whose operation is not among them is rejected before onDrop. canImport gates a drop by its offered DataFlavors - it receives the dragged data's flavors and returns whether the drop may proceed; the default accepts any flavor, so gating is opt-in. onDrop is invoked on an accepted drop with the dropped Transferable and returns whether the import succeeded.

Declaring a drop takes over the component's import: canImport and onDrop decide every drop and paste that reaches it, in place of whatever it imported on its own. Without a draggable or a clipboard on the same component, the actions it offers as a source stay the ones it offers on its own.

Requires a JComponent target. Composes with draggable and clipboard on the same component. onDrop, canImport, and acceptedActions are read on each drop, so passing fresh values across recompositions takes effect immediately.

Return

this modifier with the drop target declared on it.

Parameters

acceptedActions

the operations the target accepts, as a TransferAction mask; a clipboard paste carries no operation and clears this gate whatever it declares.

onDrop

imports the dropped data; returning false reports the transfer as failed, so a source that offered MOVE removes nothing.

canImport

gates the drop by the flavors offered; it is asked repeatedly as the pointer moves over the component, not only on the drop, so keep it cheap.

See also