java 6 - Set custom cursor when dragging external file into Swing app -
java 6 - Set custom cursor when dragging external file into Swing app -
i have swing application import external file dragging external file windows explorer onto application. have basic functionality working. however, alter default drag/drop cursor icon application appropriate cursor. have not been able alter cursor visible user while mouse key pressed , held on application. have seen examples of working if drag , drop operation within same swing application. have attempted accomplish using draggesturelistener , dragsource no avail. seems methods not called unless drag source within swing. possible alter drag cursor when dragging external file swing application?
please see 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 ) { // gets called repeatedly while dragged file on frame if ( !info.isdataflavorsupported( dataflavor.javafilelistflavor ) ) { homecoming false; } // though method called @ appropriate time, // setting cursor here of no consequence info.getcomponent().setcursor( dropcursor ); homecoming true; } @override public boolean importdata( transferhandler.transfersupport info ) { // gets called when file dropped if ( !info.isdrop() ) { homecoming false; } transferable transferable = info.gettransferable(); string importfilename = null; seek { list<file> filelist = (list<file>) transferable.gettransferdata( dataflavor.javafilelistflavor ); iterator<file> iterator = filelist.iterator(); while ( iterator.hasnext() ) { file f = iterator.next(); importfilename = f.getabsolutepath(); } info.getcomponent().setcursor( dropcursor ); thepane.setcursor( dropcursor ); } grab ( exception e ) { homecoming false; } system.out.println( "importing " + importfilename ); // homecoming cursor default thepane.setcursor( cursor.getpredefinedcursor( cursor.default_cursor ) ); homecoming true; } }
}
disclaimer: should have been comment , not answer, long fit in comments. delete reply if incorrect
i did not test looking @ api of transferhandler
suggest take @ transferhandler#getdragimage
method.
the documentation bit unclear on whether image used when drag initiated component transferhandler
set, or used when drag initiated outside of application , cursor comes on component transferhandler
set. found an example seems suggest works in java application, still inconclusive drag-and-drop coming external application
bug id 4816922 suggests utilize transferhandler#getvisualrepresentation
unclear whether bug fixed.
swing java-6
Comments
Post a Comment