当不再需要DragGestureRecognizer时,该如何处理?

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

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


这很好。但是,我需要在某个时候删除DragGestureListener才能将DnD行为恢复为默认值。我认为这只是在DragGestureListener中注销DragGestureRecognizer的问题。

但; 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...")

            }
        }

    }

}

最佳答案

您可以先取消注册它的监听器

dgr.removeDragGestureListener(dgl)


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

10-08 07:32