DragGestureRecognizer
当不再需要时,我该如何处理?
我DragSource.getDefaultDragSource().createDefaultDragGestureRecognizer()
用来确保自定义DragGestureListener
在 a 上接收拖放事件JTable
:
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...")
}
}
}
}