How to make drag drop receiving images from web browser in Java Swing? -
i trying receive images web browser via drag , drop, failing:
package tests; import javax.swing.*; import java.awt.*; import java.awt.datatransfer.dataflavor; public class jdragdroptest extends jframe { public jdragdroptest() throws headlessexception { super("dragdroptest"); setlayout(new borderlayout()); add(new jscrollpane(new jtextarea() {{ settransferhandler(new transferhandler() { @override public boolean canimport(transfersupport support) { return true; } @override public boolean importdata(transfersupport support) { int i=0; append("flavors:\n"); for( dataflavor flavor : support.getdataflavors() ) { append(string.valueof(i+1) + "): " + flavor.tostring() + "\n"); i++; } append(""); return true; } }); }}), borderlayout.center); } public static void main(string[] args) { swingutilities.invokelater(new runnable() { public void run() { jframe frame = new jdragdroptest(); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setsize(800, 600); frame.setlocationrelativeto(null); frame.setvisible(true); } }); } }
when dragging google chrome, getting typeout of 131 flavours none of image.
some of them have representation class of inputstream
, reading of them , getting text img src=
.
when dragging internet explorer, getting empty list.
when trying drag firefox, fail start drag.
os window 8.1 prof.
i image itself, not it's url, not it's img tag. possible?
here's attempt(not tested):
- i testing
windows 7
,jdk 1.8.0_51
,firefox 39.0
,chrome 45.0
,stackoverflow profile icon
.
import java.awt.*; import java.awt.datatransfer.*; import java.io.*; import java.util.list; import javax.swing.*; public class jdragdroptest2 { public jcomponent makeui() { final jlabel label = new jlabel("drop here image web browser"); label.settransferhandler(new transferhandler() { @override public boolean canimport(jcomponent component, dataflavor[] flavors) { return true; } @override public boolean importdata(jcomponent component, transferable transferable) { label.settext(""); try { (dataflavor flavor : transferable.gettransferdataflavors()) { system.out.println(flavor); if (dataflavor.imageflavor.equals(flavor)) { object o = transferable.gettransferdata(dataflavor.imageflavor); if (o instanceof image) { label.seticon(new imageicon((image) o)); return true; } } if (dataflavor.javafilelistflavor.equals(flavor)) { object o = transferable.gettransferdata(dataflavor.javafilelistflavor); if (o instanceof list) { list list = (list) o; (object f : list) { if (f instanceof file) { file file = (file) f; system.out.println(file); if (!file.getname().endswith(".bmp")) { label.seticon(new imageicon(file.getabsolutepath())); return true; } } } } } } } catch (exception ex) { ex.printstacktrace(); } return false; } @override public int getsourceactions(jcomponent component) { return copy; } }); return new jscrollpane(label); } public static void main(string... args) { eventqueue.invokelater(new runnable() { @override public void run() { createandshowgui(); } }); } public static void createandshowgui() { jframe frame = new jframe(); frame.setdefaultcloseoperation(windowconstants.exit_on_close); frame.getcontentpane().add(new jdragdroptest2().makeui()); frame.setsize(320, 240); frame.setlocationrelativeto(null); frame.setvisible(true); } }
Comments
Post a Comment