在不再需要时处理DragGestureRecognizer

时间:2013-05-13 05:55:01

标签: java swing drag-and-drop awt

如果不再需要DragGestureRecognizer,我如何处理?{/ p>

我正在使用DragSource.getDefaultDragSource().createDefaultDragGestureRecognizer()来确保自定义DragGestureListenerJTable上接收拖放事件:

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 个答案:

答案 0 :(得分:2)

你可以先取消注册它的听众

dgr.removeDragGestureListener(dgl)

你可以试试dgr.setComponent(null)。这将自动取消注册任何关联的侦听器......