java - Dealing with a DragGestureRecognizer when it is no longer needed -
how deal draggesturerecognizer
when no longer needed?
i'm using dragsource.getdefaultdragsource().createdefaultdraggesturerecognizer()
make sure custom draggesturelistener
receives drag , drop events on jtable
:
draggesturelistener dgl = new defaultdragtoreordertablegesturelistener(getview().gettrackstable()); draggesturerecognizer dgr = dragsource.getdefaultdragsource().createdefaultdraggesturerecognizer(getview().gettrackstable(), dndconstants.action_move, dgl);
this works well. however, need remove draggesturelistener @ point revert dnd behaviour default. assume question of unregistering draggesturelistener
draggesturerecognizer
.
but; do draggesturerecognizer? still registered system, can't find way unregister it? how deal draggesturerecognizer
?
package com.tracker.workspace.ui.views.test; import java.awt.dnd.dndconstants; import java.awt.dnd.draggestureevent; import java.awt.dnd.draggesturelistener; import java.awt.dnd.draggesturerecognizer; import java.awt.dnd.dragsource; import java.awt.event.actionevent; import javax.swing.abstractaction; import javax.swing.dropmode; import javax.swing.jbutton; import javax.swing.listselectionmodel; import javax.swing.transferhandler; import javax.swing.uimanager; import javax.swing.table.defaulttablemodel; public class testframe extends javax.swing.jframe { private boolean ordering = false; /** creates new form testframe */ public testframe() { initcomponents(); this.button.setaction(new orderaction(ordering)); this.table.setmodel(new defaulttablemodel() { @override public int getrowcount() { return 5; } @override public int getcolumncount() { return 1; } @override public object getvalueat(int r, int c) { return r; } }); } /** method called within constructor * initialize form. * warning: not modify code. content of method * regenerated form editor. */ @suppresswarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="generated code"> private void initcomponents() { scrollpane = new javax.swing.jscrollpane(); table = new javax.swing.jtable(); button = new javax.swing.jbutton(); setdefaultcloseoperation(javax.swing.windowconstants.exit_on_close); table.setmodel(new javax.swing.table.defaulttablemodel( new object [][] { {null, null, null, null}, {null, null, null, null}, {null, null, null, null}, {null, null, null, null} }, new string [] { "title 1", "title 2", "title 3", "title 4" } )); scrollpane.setviewportview(table); button.settext("jbutton1"); javax.swing.grouplayout layout = new javax.swing.grouplayout(getcontentpane()); getcontentpane().setlayout(layout); layout.sethorizontalgroup( layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgroup(layout.createsequentialgroup() .addcontainergap() .addgroup(layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addcomponent(scrollpane, javax.swing.grouplayout.default_size, 634, short.max_value) .addcomponent(button, javax.swing.grouplayout.preferred_size, 125, javax.swing.grouplayout.preferred_size)) .addcontainergap()) ); layout.setverticalgroup( layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgroup(layout.createsequentialgroup() .addcontainergap() .addcomponent(button) .addpreferredgap(javax.swing.layoutstyle.componentplacement.related) .addcomponent(scrollpane, javax.swing.grouplayout.default_size, 478, short.max_value) .addcontainergap()) ); pack(); }// </editor-fold> /** * @param args command line arguments */ public static void main(string args[]) { java.awt.eventqueue.invokelater(new runnable() { public void run() { // start application. try { uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname()); new testframe().setvisible(true); } catch (exception e) { e.printstacktrace(); } } }); } // variables declaration - not modify private javax.swing.jbutton button; private javax.swing.jscrollpane scrollpane; private javax.swing.jtable table; // end of variables declaration public class orderaction extends abstractaction { private draggesturerecognizer dgr; private draggesturelistener dgl; public orderaction(boolean ordering) { super(ordering ? "ordering" : "not ordering"); } public void actionperformed(actionevent ae) { ordering = !ordering; jbutton b = (jbutton) ae.getsource(); b.settext(ordering ? "ordering" : "not ordering"); if (ordering) { dgl = new draggesturelistener() { public void draggesturerecognized(draggestureevent dge) { // implementation og gesture listener goes here. } }; dgr = dragsource.getdefaultdragsource().createdefaultdraggesturerecognizer(table, dndconstants.action_move, dgl); table.setselectionmode(listselectionmodel.single_selection); table.setdragenabled(true); table.setdropmode(dropmode.insert_rows); table.settransferhandler(new transferhandler() { // implementation of transferhandler goes here. }); } else { dgr.removedraggesturelistener(dgl); // assume draggesturelistener still, somehow, registered // system. how remove "traces" of it? system.out.println("the draggesturelistener still around...") } } } }
you start unregistering it's listeners
dgr.removedraggesturelistener(dgl)
and try dgr.setcomponent(null)
. automatically unregister associated listeners...
Comments
Post a Comment