swing - Java, drag and drop to change the order of panels -
ok have set follows. have java 1.7 project uses javax.swing ui. first project outside of following tutorials, initiative tracker familiar table top. have class extends jpanel (and implements comparable<>) can add dynamically , story different data each class.
the panels added layout, y position depending on how many panels there. added arraylist sort them in order. list can sorted 1 of properties of class.
what running want manually able drag , drop them change order, both in layout , in arraylist. added note can't remove , re-add 1 because lose values saved in class.
toward end made jbutton needs following:
- so long mouse pressed down on button, panel belongs needs dragged mouse.
- when mouse released needs check position , if necessary go spot.
i have tried drop , drag tutorials can't seem find 1 can adjust want.
edit1: tried.
mouselistener listener = new dragmouseadapter(); dragbutton.addmouselistener(listener); dragbutton.settransferhandler(new transferhandler("text"));
and then
class dragmouseadapter extends mouseadapter { public void mousepressed(mouseevent e) { jcomponent c = (jcomponent) e.getsource(); transferhandler handler = c.gettransferhandler(); handler.exportasdrag(c, e, transferhandler.copy); // todo make swap location drag , drop } }
this tutorial , succesfully changes text , not @ want , don't know how change want.
edit2: this tutorial describes want do, except doesn't work. gives following error:
step 1 of 7: mouse pressed. going export our randomdraganddroppanel draggable. step 2 of 7: returning acceptable transferhandler action. our randomdraganddroppanel accepts copy only. step 3 of 7: casting randomdraganddroppanel transferable. transferable randomdraganddroppanel queried acceptable dataflavors enters drop targets, present target object transfers. step 4 of 7: querying acceptable dataflavors determine available. our example supports our custom randomdraganddroppanel dataflavor. problem lazy loading: randomdraganddroppanel java.lang.classnotfoundexception: randomdraganddroppanel @ java.lang.class.forname0(native method) @ java.lang.class.forname(unknown source) @ java.awt.datatransfer.dataflavor.trytoloadclass(unknown source) @ java.awt.datatransfer.dataflavor.initialize(unknown source) @ java.awt.datatransfer.dataflavor.<init>(unknown source) @ dnd.draganddroppanelsdemo.getdraganddroppaneldataflavor(draganddroppanelsdemo.java:197) @ dnd.randomdraganddroppanel.gettransferdataflavors(draganddroppanelsdemo.java:392) @ sun.awt.datatransfer.datatransferer.getformatsfortransferable(unknown source) @ sun.awt.dnd.sundragsourcecontextpeer.startdrag(unknown source) @ java.awt.dnd.dragsource.startdrag(unknown source) @ java.awt.dnd.dragsource.startdrag(unknown source) @ java.awt.dnd.draggestureevent.startdrag(unknown source) @ javax.swing.transferhandler$draghandler.draggesturerecognized(unknown source) @ java.awt.dnd.draggesturerecognizer.firedraggesturerecognized(unknown source) @ javax.swing.transferhandler$swingdraggesturerecognizer.gestured(unknown source) @ javax.swing.transferhandler.exportasdrag(unknown source) @ dnd.draggablemouselistener.mousepressed(draganddroppanelsdemo.java:456) @ java.awt.component.processmouseevent(unknown source) @ javax.swing.jcomponent.processmouseevent(unknown source) @ java.awt.component.processevent(unknown source) @ java.awt.container.processevent(unknown source) @ java.awt.component.dispatcheventimpl(unknown source) @ java.awt.container.dispatcheventimpl(unknown source) @ java.awt.component.dispatchevent(unknown source) @ java.awt.lightweightdispatcher.retargetmouseevent(unknown source) @ java.awt.lightweightdispatcher.processmouseevent(unknown source) @ java.awt.lightweightdispatcher.dispatchevent(unknown source) @ java.awt.container.dispatcheventimpl(unknown source) @ java.awt.window.dispatcheventimpl(unknown source) @ java.awt.component.dispatchevent(unknown source) @ java.awt.eventqueue.dispatcheventimpl(unknown source) @ java.awt.eventqueue.access$200(unknown source) @ java.awt.eventqueue$3.run(unknown source) @ java.awt.eventqueue$3.run(unknown source) @ java.security.accesscontroller.doprivileged(native method) @ java.security.protectiondomain$1.dointersectionprivilege(unknown source) @ java.security.protectiondomain$1.dointersectionprivilege(unknown source) @ java.awt.eventqueue$4.run(unknown source) @ java.awt.eventqueue$4.run(unknown source) @ java.security.accesscontroller.doprivileged(native method) @ java.security.protectiondomain$1.dointersectionprivilege(unknown source) @ java.awt.eventqueue.dispatchevent(unknown source) @ java.awt.eventdispatchthread.pumponeeventforfilters(unknown source) @ java.awt.eventdispatchthread.pumpeventsforfilter(unknown source) @ java.awt.eventdispatchthread.pumpeventsforhierarchy(unknown source) @ java.awt.eventdispatchthread.pumpevents(unknown source) @ java.awt.eventdispatchthread.pumpevents(unknown source) @ java.awt.eventdispatchthread.run(unknown source)
here example of drag , drop change order of panels(does not support sorting).
dragmouseadapter#mousedragged(...)
: search drag panel, remove form parent panel , open cursor window.dragmouseadapter#mousereleased(...)
: search drop location, insert dragging panel, close cursor window.
import java.awt.*; import java.awt.dnd.*; import java.awt.event.*; import java.net.*; import java.util.*; import javax.swing.*; public class rearrangeorderofpanelstest2 { public jcomponent makeui() { box box = box.createverticalbox(); dragmouseadapter dh = new dragmouseadapter(); box.addmouselistener(dh); box.addmousemotionlistener(dh); int idx = 0; (jcomponent c : arrays.aslist( new jlabel("<html>111<br>11<br>11"), new jbutton("2"), new jcheckbox("3"), new jtextfield(14))) { box.add(createtoolbarbutton(idx++, c)); } jpanel p = new jpanel(new borderlayout()); p.add(box, borderlayout.north); return p; } private static jcomponent createtoolbarbutton(int i, jcomponent c) { jlabel l = new jlabel(string.format(" %04d ", i)); l.setopaque(true); l.setbackground(color.red); jpanel p = new jpanel(new borderlayout()); p.setborder(borderfactory.createcompoundborder( borderfactory.createemptyborder(5, 5, 5, 5), borderfactory.createlineborder(color.blue, 2))); p.add(l, borderlayout.west); p.add(c); p.setopaque(false); return p; } public static void main(string... args) { eventqueue.invokelater(new runnable() { @override public void run() { createandshowgui(); } }); } public static void createandshowgui() { jframe f = new jframe(); f.setdefaultcloseoperation(windowconstants.exit_on_close); f.getcontentpane().add(new rearrangeorderofpanelstest2().makeui()); f.setsize(320, 240); f.setlocationrelativeto(null); f.setvisible(true); } } class dragmouseadapter extends mouseadapter { private static final int xoffset = 16; private static final rectangle r1 = new rectangle(); private static final rectangle r2 = new rectangle(); private static rectangle prevrect; private final jwindow window = new jwindow(); private component draggingcomonent; private int index = -1; private component gap; private point startpt; private point dragoffset; private final int gesturemotionthreshold = dragsource.getdragthreshold(); public dragmouseadapter() { super(); window.setbackground(new color(0, true)); } @override public void mousepressed(mouseevent e) { jcomponent parent = (jcomponent) e.getcomponent(); if (parent.getcomponentcount() <= 1) { startpt = null; return; } startpt = e.getpoint(); } private void startdragging(jcomponent parent, point pt) { //get dragging panel component c = parent.getcomponentat(pt); index = parent.getcomponentzorder(c); if (objects.equals(c, parent) || index < 0) { return; } draggingcomonent = c; dimension d = draggingcomonent.getsize(); point dp = draggingcomonent.getlocation(); dragoffset = new point(pt.x - dp.x, pt.y - dp.y); //make dummy filler gap = box.createrigidarea(d); swapcomponentlocation(parent, c, gap, index); //make cursor window window.add(draggingcomonent); window.pack(); updatewindowlocation(pt, parent); window.setvisible(true); } private void updatewindowlocation(point pt, jcomponent parent) { point p = new point(pt.x - dragoffset.x, pt.y - dragoffset.y); swingutilities.convertpointtoscreen(p, parent); window.setlocation(p); } private static int gettargetindex(rectangle r, point pt, int i) { int ht2 = (int)(.5 + r.height * .5); r1.setbounds(r.x, r.y, r.width, ht2); r2.setbounds(r.x, r.y + ht2, r.width, ht2); if (r1.contains(pt)) { prevrect = r1; return - 1 > 0 ? : 0; } else if (r2.contains(pt)) { prevrect = r2; return i; } return -1; } private static void swapcomponentlocation( container parent, component remove, component add, int idx) { parent.remove(remove); parent.add(add, idx); parent.revalidate(); parent.repaint(); } @override public void mousedragged(mouseevent e) { point pt = e.getpoint(); jcomponent parent = (jcomponent) e.getcomponent(); //motionthreshold double = math.pow(pt.x - startpt.x, 2); double b = math.pow(pt.y - startpt.y, 2); if (draggingcomonent == null && math.sqrt(a + b) > gesturemotionthreshold) { startdragging(parent, pt); return; } if (!window.isvisible() || draggingcomonent == null) { return; } //update cursor window location updatewindowlocation(pt, parent); if (prevrect != null && prevrect.contains(pt)) { return; } //change dummy filler location (int = 0; < parent.getcomponentcount(); i++) { component c = parent.getcomponent(i); rectangle r = c.getbounds(); if (objects.equals(c, gap) && r.contains(pt)) { return; } int tgt = gettargetindex(r, pt, i); if (tgt >= 0) { swapcomponentlocation(parent, gap, gap, tgt); return; } } parent.remove(gap); parent.revalidate(); } @override public void mousereleased(mouseevent e) { startpt = null; if (!window.isvisible() || draggingcomonent == null) { return; } point pt = e.getpoint(); jcomponent parent = (jcomponent) e.getcomponent(); //close cursor window component cmp = draggingcomonent; draggingcomonent = null; prevrect = null; startpt = null; dragoffset = null; window.setvisible(false); //swap dragging panel , dummy filler (int = 0; < parent.getcomponentcount(); i++) { component c = parent.getcomponent(i); if (objects.equals(c, gap)) { swapcomponentlocation(parent, gap, cmp, i); return; } int tgt = gettargetindex(c.getbounds(), pt, i); if (tgt >= 0) { swapcomponentlocation(parent, gap, cmp, tgt); return; } } if (parent.getparent().getbounds().contains(pt)) { swapcomponentlocation(parent, gap, cmp, parent.getcomponentcount()); } else { swapcomponentlocation(parent, gap, cmp, index); } } }
Comments
Post a Comment