Java Native Access Platform provides cross-platform mappings and utilities for commonly used platform functions across Windows, macOS, and Linux systems.
—
JNA Platform provides enhanced drag and drop functionality that extends Java's built-in support with cross-platform ghosted drag images and simplified APIs. This is particularly useful when native drag image support is unavailable or inconsistent across platforms.
Abstract class that provides simplified drag handling for components with ghosted drag images and enhanced visual feedback.
/**
* Provides simplified drag handling for components with ghosted drag images
* Abstract class requiring implementation of drag gesture handling
*/
public abstract class DragHandler {
/** Maximum size for ghosted drag images */
public static final Dimension MAX_GHOST_SIZE = new Dimension(250, 250);
/** Default alpha transparency for ghosted images */
public static final float DEFAULT_GHOST_ALPHA = 0.5f;
/** Constant indicating unknown modifier keys */
public static final int UNKNOWN_MODIFIERS = -1;
/** Constant for unknown transferable data */
public static final Object UNKNOWN_TRANSFERABLE = null;
/**
* Create drag handler for component
* @param dragSource Component that can be dragged
* @param actions Supported drag actions (DnDConstants.ACTION_*)
*/
protected DragHandler(Component dragSource, int actions);
/**
* Utility method to extract transferable data from drop target events
* @param e Drop target event
* @return Transferable data from the event
*/
public static Transferable getTransferable(DropTargetEvent e);
}Abstract class that provides simplified drop handling for components with optional visual feedback through custom painters.
/**
* Provides simplified drop handling for components
* Abstract class that implements DropTargetListener
*/
public abstract class DropHandler implements DropTargetListener {
/**
* Create drop handler for component
* @param dropTarget Component that can receive drops
* @param actions Supported drop actions (DnDConstants.ACTION_*)
*/
public DropHandler(Component dropTarget, int actions);
/**
* Create drop handler with data flavor restrictions
* @param dropTarget Component that can receive drops
* @param actions Supported drop actions
* @param flavors Accepted data flavors
*/
public DropHandler(Component dropTarget, int actions, DataFlavor[] flavors);
/**
* Create drop handler with custom painter for visual feedback
* @param dropTarget Component that can receive drops
* @param actions Supported drop actions
* @param flavors Accepted data flavors
* @param painter Custom painter for drop feedback
*/
public DropHandler(Component dropTarget, int actions, DataFlavor[] flavors, DropTargetPainter painter);
/**
* Check if drop handling is currently active
* @return true if drop handling is active
*/
public boolean isActive();
/**
* Enable or disable drop handling
* @param active true to enable, false to disable
*/
public void setActive(boolean active);
}Interface for providing custom visual feedback during drop operations.
/**
* Callback interface for customizing drop target visual feedback
* Allows painting custom indicators when items are dragged over drop targets
*/
public interface DropTargetPainter {
/**
* Paint custom drop target feedback
* @param e Drop target event providing context
* @param action Current drop action being performed
* @param location Current cursor location relative to component
*/
void paintDropTarget(DropTargetEvent e, int action, Point location);
}Utility class that provides translucent drag images when native platform support is unavailable, using window transparency effects from WindowUtils.
/**
* Provides ghosted drag images when native drag image support is unavailable
* Uses WindowUtils for transparency effects
*/
public class GhostedDragImage {
/**
* Create ghosted drag image with icon
* @param dragSource Source component for the drag operation
* @param icon Icon to use for the drag image
* @param initialScreenLoc Initial screen location for the image
* @param cursorOffset Offset from cursor to image corner
*/
public GhostedDragImage(Component dragSource, Icon icon, Point initialScreenLoc, Point cursorOffset);
/**
* Set transparency level of the ghosted image
* @param alpha Alpha transparency (0.0f = transparent, 1.0f = opaque)
*/
public void setAlpha(float alpha);
/**
* Clean up resources and dispose of the ghosted image
*/
public void dispose();
/**
* Move the ghosted image to new screen location
* @param screenLocation New screen coordinates for the image
*/
public void move(Point screenLocation);
/**
* Animate the ghosted image returning to its original position
*/
public void returnToOrigin();
}import com.sun.jna.platform.dnd.DragHandler;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DragGestureEvent;
// Create custom drag handler for a JLabel with an image
DragHandler dragHandler = new DragHandler(imageLabel, DnDConstants.ACTION_COPY) {
@Override
public void dragGestureRecognized(DragGestureEvent e) {
// Start drag operation with transferable data
ImageIcon icon = (ImageIcon) imageLabel.getIcon();
Transferable transferable = new ImageTransferable(icon.getImage());
e.startDrag(null, transferable, this);
}
@Override
public void dragEnter(DragSourceDragEvent e) {
// Handle drag enter
}
// Implement other DragSourceListener methods...
};import com.sun.jna.platform.dnd.DropHandler;
import com.sun.jna.platform.dnd.DropTargetPainter;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DropTargetDropEvent;
// Create drop handler with custom visual feedback
DropHandler dropHandler = new DropHandler(
targetPanel,
DnDConstants.ACTION_COPY,
new DataFlavor[]{DataFlavor.imageFlavor},
new DropTargetPainter() {
@Override
public void paintDropTarget(DropTargetEvent e, int action, Point location) {
// Paint custom drop indicator
Graphics2D g2 = (Graphics2D) e.getDropTargetContext().getComponent().getGraphics();
g2.setColor(Color.BLUE);
g2.drawRect(location.x - 10, location.y - 10, 20, 20);
}
}
) {
@Override
public void drop(DropTargetDropEvent e) {
try {
// Accept the drop
e.acceptDrop(DnDConstants.ACTION_COPY);
// Get the transferred data
Image image = (Image) e.getTransferable().getTransferData(DataFlavor.imageFlavor);
// Process the dropped image
processDroppedImage(image);
// Mark drop as complete
e.dropComplete(true);
} catch (Exception ex) {
e.dropComplete(false);
}
}
};import com.sun.jna.platform.dnd.GhostedDragImage;
// Create ghosted image during drag operation
Point initialLocation = e.getDragOrigin();
SwingUtilities.convertPointToScreen(initialLocation, dragSource);
GhostedDragImage ghostImage = new GhostedDragImage(
dragSource,
dragIcon,
initialLocation,
new Point(16, 16) // Offset from cursor
);
// Set custom transparency
ghostImage.setAlpha(0.7f);
// Move image during drag (typically in dragOver event)
Point newLocation = getCurrentScreenLocation();
ghostImage.move(newLocation);
// Clean up when drag ends
ghostImage.dispose();/contrib/dnddemo/ in the JNA source for a complete working exampleThe drag and drop support requires:
Install with Tessl CLI
npx tessl i tessl/maven-net-java-dev-jna--jna-platform