I have a swing application in which I would like to import an external file by dragging an external file from Windows Explorer to the application. I have this basic functionality. However, I would like to change the default mouse cursor icon to the corresponding application cursor. I could not change the cursor visible to the user when the mouse button is pressed and held over the application. I saw examples of this work if the drag operation is in the same swing application. I tried to accomplish this using DragGestureListener and DragSource to no avail. It seems that these methods are not called if the source of resistance is not within the range of oscillation. Is it possible to change the drag and drop cursor while dragging an external file into the swing application?
Please see this simplified example:
public class DnDTemplate extends JFrame { private static final long serialVersionUID = 1L; private JComponent thePane = null; private Cursor dropCursor = null; public DnDTemplate() { super( "Drop File Here" ); thePane = (JComponent) getContentPane(); thePane.setTransferHandler( new DndTransferHandler() ); ImageIcon imageIcon = new ImageIcon( "drop_here.gif" ); Image image = imageIcon.getImage(); BufferedImage bufferedImage = new BufferedImage( 16, 16, BufferedImage.TYPE_INT_ARGB ); Graphics graphics = bufferedImage.getGraphics(); graphics.drawImage( image, 0, 0, null ); dropCursor = Toolkit.getDefaultToolkit().createCustomCursor( bufferedImage, new Point( 16, 16 ), "drop cursor" ); setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); setSize( 300, 300 ); } public static void main( String[] args ) { new DnDTemplate().setVisible( true ); } class DndTransferHandler extends TransferHandler { private static final long serialVersionUID = 1L; @Override public boolean canImport( TransferHandler.TransferSupport info ) {
}
agility
source share