2013-05-13 58 views
0

如何在不再需要时处理DragGestureRecognizer在不再需要DragGestureRecognizer时处理

我使用DragSource.getDefaultDragSource().createDefaultDragGestureRecognizer()确保定制DragGestureListener接收关于JTable拖放事件:

DragGestureListener dgl = new DefaultDragToReorderTableGestureListener(getView().getTracksTable()); 
DragGestureRecognizer dgr = DragSource.getDefaultDragSource().createDefaultDragGestureRecognizer(getView().getTracksTable(), DnDConstants.ACTION_MOVE, dgl); 

这工作得很好。但是,我需要在某个时候删除DragGestureListener以将DnD行为恢复为默认行为。我认为这只是一个注销DragGestureListenerDragGestureRecognizer的问题。

但是;我该如何处理DragGestureRecognizer?它仍然在系统中注册,但我找不到任何注销它的方法?我如何处理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; 
      } 



     }); 
    } 

    /** This method is called from within the constructor to 
    * initialize the form. 
    * WARNING: Do NOT modify this code. The content of this method is 
    * always regenerated by the 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 the 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 - do 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); 

       // I assume the DragGestureListener is still, somehow, registered 
       // with the system. How do I remove all "traces" of it? 
       System.out.println("The DragGestureListener is still around...") 

      } 
     } 

    } 

} 
+1

为了更好地提供帮助,请发布[SSCCE](http://sscce.org/)。 – 2013-05-13 06:00:41

+0

@AndrewThompson:SSCCE张贴。 – sbrattla 2013-05-13 06:11:11

回答

2

你可以通过注销它启动的听众

dgr.removeDragGestureListener(dgl) 

而且你可以尝试dgr.setComponent(null)。这将自动取消注册任何相关的听众...

+0

谢谢。那就是了?它应该将DragGestureRecognizer标记为GC的候选者(假设我还没有创建任何其他引用)? – sbrattla 2013-05-13 07:09:23

+0

就我所知,是的 – MadProgrammer 2013-05-13 07:12:04

+0

似乎是合理的。我会解决这个问题。感谢您的输入! – sbrattla 2013-05-13 07:12:39