int actions = DnDConstants.MOVE_OR_COPY;
Component component = ...;
DropHandler handler = new DropHandler(component, actions);
DragHandler and the user explicitly requests a MOVE operation;
this is indistinguishable from a drag with no modifiers unless we have
access to the key modifiers, which DragHandler provides).
canDrop.
paintDropTarget(DropTargetEvent, int, Point) or by providing
a DropTargetPainter.
getDropAction(DropTargetEvent) follows these steps to
determine the appropriate action (if any).
isSupported(DataFlavor[]) determines if there are any supported
flavors
getDropActionsForFlavors(DataFlavor[]) reduces the supported
actions based on available flavors. For instance, a text field for file
paths might support DnDConstants#ACTION_COPY_OR_MOVE on a plain
string, but DnDConstants#ACTION_LINK might be the only action
supported on a file.
getDropAction(DropTargetEvent, int, int, int) relax the action
if it's the default, or restrict it for user requested actions.
canDrop(DropTargetEvent, int, Point) change the action based on
the location in the drop target component, or any other criteria.
drop(DropTargetDropEvent, int) to handle the drop.
You should invoke DropTargetDropEvent#dropComplete as soon
as the Transferable data is obtained, to avoid making the DnD
operation look suspended.
DragHandler
| Modifier and Type | Field and Description |
|---|---|
| private int | |
| private List | |
| private boolean | |
| private DropTarget | |
| private String | |
| private DropTargetPainter |
| Access | Constructor and Description |
|---|---|
| public | |
| public | DropHandler(final Component
The component to receive drops c, int Allowed actions for drops acceptedActions, DataFlavor[] Allowed data flavors for drops acceptedFlavors)Enable handling of drops, indicating what actions and flavors are acceptable. |
| public | DropHandler(final Component
The component to receive drops c, int Allowed actions for drops acceptedActions, DataFlavor[] Allowed data flavors for drops acceptedFlavors, DropTargetPainter Painter to handle drop target feedback painter)Enable handling of drops, indicating what actions and flavors are acceptable, and providing a painter for drop target feedback. |
| Modifier and Type | Method and Description |
|---|---|
| protected int | |
| protected boolean | canDrop(DropTargetEvent e, int action, Point location)
Indicate whether the given drop action is acceptable at the given location. |
| private void | |
| public void | dragEnter(DropTargetDragEvent
the e)DropTargetDragEventImplements java. DropTarget
registered with this listener.
|
| public void | dragExit(DropTargetEvent
the e)DropTargetEventImplements java. DropTarget registered with this listener.
|
| public void | dragOver(DropTargetDragEvent
the e)DropTargetDragEventImplements java. DropTarget
registered with this listener.
|
| public void | drop(DropTargetDropEvent
the e)DropTargetDropEventImplements java. |
| protected abstract void | |
| public void | dropActionChanged(DropTargetDragEvent
the e)DropTargetDragEventImplements java. |
| protected int | |
| protected int | |
| protected int | getDropActionsForFlavors(DataFlavor[]
currently available flavors dataFlavors)Indicate the actions available for the given list of data flavors. |
| protected DropTarget | |
| public boolean | |
| protected boolean | |
| protected boolean | modifiersActive(int dropAction)
Returns whether there are key modifiers active , or false if they can't be determined. |
| protected void | paintDropTarget(DropTargetEvent
The drop target event e, int The action for the drop action, Point The intended drop location, or null if there is none location)Update the appearance of the target component. |
| public void |
| acceptedActions | back to summary |
|---|---|
| private int acceptedActions | |
| acceptedFlavors | back to summary |
|---|---|
| private List<E> acceptedFlavors | |
| active | back to summary |
|---|---|
| private boolean active | |
| dropTarget | back to summary |
|---|---|
| private DropTarget dropTarget | |
| lastAction | back to summary |
|---|---|
| private String lastAction | |
| painter | back to summary |
|---|---|
| private DropTargetPainter painter | |
| DropHandler | back to summary |
|---|---|
| public DropHandler(Component c, int acceptedActions) Create a handler that allows the given set of actions. If using
this constructor, you will need to override | |
| DropHandler | back to summary |
|---|---|
| public DropHandler(final Component c, int acceptedActions, DataFlavor[] acceptedFlavors) Enable handling of drops, indicating what actions and flavors are acceptable.
| |
| DropHandler | back to summary |
|---|---|
| public DropHandler(final Component c, int acceptedActions, DataFlavor[] acceptedFlavors, DropTargetPainter painter) Enable handling of drops, indicating what actions and flavors are acceptable, and providing a painter for drop target feedback.
| |
| acceptOrReject | back to summary |
|---|---|
| protected int acceptOrReject(DropTargetDragEvent e) Accept or reject the drag represented by the given event. Returns
the action determined by | |
| canDrop | back to summary |
|---|---|
| protected boolean canDrop(DropTargetEvent e, int action, Point location) Indicate whether the given drop action is acceptable at the given
location. This method is the last check performed by
| |
| describe | back to summary |
|---|---|
| private void describe(String type, DropTargetEvent e) | |
| dragEnter | back to summary |
|---|---|
| public void dragEnter(DropTargetDragEvent e) Implements java. Doc from java. Called while a drag operation is ongoing, when the mouse pointer enters
the operable part of the drop site for the
| |
| dragExit | back to summary |
|---|---|
| public void dragExit(DropTargetEvent e) Implements java. Doc from java. Called while a drag operation is ongoing, when the mouse pointer has
exited the operable part of the drop site for the
| |
| dragOver | back to summary |
|---|---|
| public void dragOver(DropTargetDragEvent e) Implements java. Doc from java. Called when a drag operation is ongoing, while the mouse pointer is still
over the operable part of the drop site for the
| |
| drop | back to summary |
|---|---|
| public void drop(DropTargetDropEvent e) Implements java. Indicates the user has initiated a drop. The default performs all
standard drop validity checking and handling, then invokes
| |
| drop | back to summary |
|---|---|
| protected abstract void drop(DropTargetDropEvent e, int action) throws UnsupportedFlavorException, IOException Handle an incoming drop with the given action. The action passed in
might be different from | |
| dropActionChanged | back to summary |
|---|---|
| public void dropActionChanged(DropTargetDragEvent e) Implements java. Doc from java. Called if the user has modified the current drop gesture.
| |
| getDropAction | back to summary |
|---|---|
| protected int getDropAction(DropTargetEvent e) Calculate the effective action. The default implementation
checks whether any
| |
| getDropAction | back to summary |
|---|---|
| protected int getDropAction(DropTargetEvent e, int currentAction, int sourceActions, int acceptedActions) | |
| getDropActionsForFlavors | back to summary |
|---|---|
| protected int getDropActionsForFlavors(DataFlavor[] dataFlavors) Indicate the actions available for the given list of data flavors.
Override this method if the acceptable drop actions depend
on the currently available
| |
| getDropTarget | back to summary |
|---|---|
| protected DropTarget getDropTarget() | |
| isActive | back to summary |
|---|---|
| public boolean isActive() Whether this drop target is active. | |
| isSupported | back to summary |
|---|---|
| protected boolean isSupported(DataFlavor[] flavors) Return whether any of the flavors in the given list are accepted. The list is compared against the accepted list provided in the constructor. | |
| modifiersActive | back to summary |
|---|---|
| protected boolean modifiersActive(int dropAction) Returns whether there are key modifiers active , or false if they can't be determined. We use the DragHandler hint, if available, or fall back to whether the drop action is other than the default (move). | |
| paintDropTarget | back to summary |
|---|---|
| protected void paintDropTarget(DropTargetEvent e, int action, Point location) Update the appearance of the target component. Normally the decoration
should be painted only if the event is an instance of
For an easy way to highlight the drop target, consider using a single
instance of
| |
| setActive | back to summary |
|---|---|
| public void setActive(boolean active) Set whether this handler (and thus its drop target) will accept any drops. | |