gpt4 book ai didi

java - 当不再需要 DragGestureRecognizer 时对其进行处理

转载 作者:行者123 更新时间:2023-12-02 07:02:08 25 4
gpt4 key购买 nike

我该如何处理 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 行为恢复为默认值。我认为这只是注销 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)。这将自动取消注册任何关联的监听器...

关于java - 当不再需要 DragGestureRecognizer 时对其进行处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16515627/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com