java - Drop everywhere in a jpanel -
based on example : https://stackoverflow.com/a/17359895/3259386
i have 2 panels, 1 drag , other drop, drag copy , don't move dragged image.
the code is...
import java.awt.borderlayout; import java.awt.component; import java.awt.dimension; import java.awt.point; import java.awt.datatransfer.dataflavor; import java.awt.datatransfer.transferable; import java.awt.datatransfer.unsupportedflavorexception; import java.awt.dnd.dndconstants; import java.awt.dnd.draggestureevent; import java.awt.dnd.draggesturelistener; import java.awt.dnd.dragsource; import java.awt.dnd.droptarget; import java.awt.dnd.droptargetadapter; import java.awt.dnd.droptargetdropevent; import java.io.ioexception; import java.net.url; import javax.swing.icon; import javax.swing.imageicon; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; import javax.swing.swingutilities; import javax.swing.transferhandler; import javax.swing.transferhandler.transfersupport; import javax.swing.border.titledborder; public class test { public static void main(string[] args) { createandshowjframe(); } public static void createandshowjframe() { swingutilities.invokelater(new runnable() { @override public void run() { jframe frame = createjframe(); frame.setvisible(true); } }); } private static jframe createjframe() { jframe frame = new jframe(); frame.setdefaultcloseoperation(jframe.dispose_on_close); frame.settitle("test"); jpanel panel = createemptyjpanel(); new mydroptargetlistener(panel);//this must done or wont able drop image onto empty panel frame.add(panel, borderlayout.center); try { frame.add(createjlabelpanel(), borderlayout.south); } catch (exception ex) { ex.printstacktrace(); } frame.pack(); return frame; } private static jpanel createemptyjpanel() { final jpanel p = new jpanel() { @override public dimension getpreferredsize() { return new dimension(300, 300); } }; p.setborder(new titledborder("drag image onto panel")); transferhandler dnd = new transferhandler() { @override public boolean canimport(transfersupport support) { if (!support.isdrop()) { return false; } //only strings if (!support.isdataflavorsupported(dataflavor.imageflavor)) { return false; } return true; } @override public boolean importdata(transfersupport support) { if (!canimport(support)) { return false; } transferable tansferable = support.gettransferable(); icon ico; try { ico = (icon) tansferable.gettransferdata(dataflavor.imageflavor); } catch (exception e) { e.printstacktrace(); return false; } p.add(new jlabel(ico)); return true; } }; p.settransferhandler(dnd); return p; } private static jpanel createjlabelpanel() throws exception { jpanel panel = new jpanel(); panel.setborder(new titledborder("drag image here panel above")); jlabel label1 = new jlabel(new imageicon(new url("http://i.stack.imgur.com/gjmej.png"))); jlabel label2 = new jlabel(new imageicon(new url("http://i.stack.imgur.com/8bgfi.png"))); jlabel label3 = new jlabel(new imageicon(new url("http://i.stack.imgur.com/1lgtq.png"))); mydraggesturelistener dlistener = new mydraggesturelistener(); dragsource ds1 = new dragsource(); ds1.createdefaultdraggesturerecognizer(label1, dndconstants.action_copy, dlistener); dragsource ds2 = new dragsource(); ds2.createdefaultdraggesturerecognizer(label2, dndconstants.action_copy, dlistener); dragsource ds3 = new dragsource(); ds3.createdefaultdraggesturerecognizer(label3, dndconstants.action_copy, dlistener); panel.add(label1); panel.add(label2); panel.add(label3); return panel; } } class mydroptargetlistener extends droptargetadapter { private droptarget droptarget; private jpanel p; public mydroptargetlistener(jpanel panel) { p = panel; droptarget = new droptarget(panel, dndconstants.action_copy, this, true, null); } @override public void drop(droptargetdropevent event) { try { droptarget test = (droptarget) event.getsource(); component ca = (component) test.getcomponent(); point droppoint = ca.getmouseposition(); transferable tr = event.gettransferable(); if (event.isdataflavorsupported(dataflavor.imageflavor)) { icon ico = (icon) tr.gettransferdata(dataflavor.imageflavor); if (ico != null) { p.add(new jlabel(ico)); p.revalidate(); p.repaint(); event.dropcomplete(true); } } else { event.rejectdrop(); } } catch (exception e) { e.printstacktrace(); event.rejectdrop(); } } } class mydraggesturelistener implements draggesturelistener { @override public void draggesturerecognized(draggestureevent event) { jlabel label = (jlabel) event.getcomponent(); final icon ico = label.geticon(); transferable transferable = new transferable() { @override public dataflavor[] gettransferdataflavors() { return new dataflavor[]{dataflavor.imageflavor}; } @override public boolean isdataflavorsupported(dataflavor flavor) { if (!isdataflavorsupported(flavor)) { return false; } return true; } @override public object gettransferdata(dataflavor flavor) throws unsupportedflavorexception, ioexception { return ico; } }; event.startdrag(null, transferable); } }
when drop element on top panel, element placed in center this
i want drop everywhere on top panel.
when set top panel null layout : p.setlayout(null);
, dropped image don't show anymore.
when drop element on top panel, element placed in center this
the default layout manager panel flowlayout.
when set top panel null layout ... dropped image don't show anymore.
when use null layout responsible setting size , location of component.
//p.add(new jlabel(ico)); jlabel label = new jlabel(ico); label.setsize( label.getpreferredsize()); label.setlocation(...); p.add(label);
you can use transfersupport
class droplocation
class contains drop point can use set location of label.
Comments
Post a Comment