我已经为 JTableHeader 实现了拖放,因此当用户单击按下 Ctrl 的表的标题而不是重新排序列时,将传输自定义对象。
它看起来工作正常,但是当调用 DragGestureEvent 的 startDrag 方法时,被拖动的列不合适(参见 column4 和 column5 之间的间隙):
如果在那之后单击该列或在不使用 Ctrl 键的情况下拖动它,它将返回到正确的位置。有谁知道让专栏保持其位置的方法?
源代码:
public class Tree {
private final String name;
public Tree (final String name) {
this.name = name;
}
public String getName() {
return name;
}
@Override
public String toString() {
return "Tree[" + name + "]";
};
}
和这个:
import java.awt.BorderLayout;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DragGestureEvent;
import java.awt.dnd.DragGestureListener;
import java.awt.dnd.DragSource;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.io.IOException;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.TransferHandler;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumnModel;
public class DnDTest extends JPanel {
private static final long serialVersionUID = 8341231933303004329L;
private static final DataFlavor FLAVOR = new DataFlavor(Tree.class,
Tree.class.getSimpleName());
private final TableTransferHandler<Integer> customHandler = new TableTransferHandler<Integer>();
/* ************** CONSTRUCTORS *************** */
public DnDTest() {
setLayout(new GridLayout(1, 2));
final JTable table = createTable();
table.setTableHeader(new CustomTableHeader(table.getColumnModel()));
final JPanel right = new JPanel(new BorderLayout());
final JLabel dropLabel = new JLabel();
dropLabel.setTransferHandler(customHandler);
right.add(dropLabel);
this.add(right);
new DragSource().createDefaultDragGestureRecognizer(
table.getTableHeader(), DnDConstants.ACTION_COPY,
new CustomDragGesture());
}
/* ************* PRIVATE METHODS ************* */
private JTable createTable() {
final DefaultTableModel model = createTableModel();
final JTable table = new JTable(model);
this.add(new JScrollPane(table));
return table;
}
private DefaultTableModel createTableModel() {
final String[] columnNames = { "Column1", //$NON-NLS-1$
"Column2", //$NON-NLS-1$
"Column3", //$NON-NLS-1$
"Column4", //$NON-NLS-1$
"Column5" //$NON-NLS-1$
};
final Integer[][] data = new Integer[1][columnNames.length];
for (int i = 0; i < data.length; i++) {
for (int j = 0; j < data[i].length; j++) {
data[i][j] = (int) (Math.random() * 30);
}
}
final DefaultTableModel model = new DefaultTableModel(data, columnNames);
return model;
}
/* ***************** CLASSES ***************** */
private static final class CustomTableHeader extends JTableHeader {
private static final long serialVersionUID = -781646896813653894L;
public CustomTableHeader(final TableColumnModel columnModel) {
super(columnModel);
}
public Tree getTree() {
return new Tree("Test tree");
}
}
private static final class CustomDragGesture implements DragGestureListener {
@Override
public void dragGestureRecognized(DragGestureEvent event) {
if (!(event.getTriggerEvent() instanceof MouseEvent && SwingUtilities
.isLeftMouseButton((MouseEvent) event.getTriggerEvent()))
|| (event.getTriggerEvent().getModifiers() & ActionEvent.CTRL_MASK) != ActionEvent.CTRL_MASK)
return;
final CustomTableHeader tableHeader = (CustomTableHeader) event
.getComponent();
Cursor cursor = null;
if (event.getDragAction() == DnDConstants.ACTION_COPY) {
cursor = DragSource.DefaultCopyDrop;
}
final Tree tree = tableHeader.getTree();
event.startDrag(cursor, new TransferableTree(tree));
}
}
private static final class TransferableTree extends Tree implements
Transferable {
private final Tree tree;
public TransferableTree(final Tree tree) {
super(tree.getName());
this.tree = tree;
}
@Override
public DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[] { FLAVOR };
}
@Override
public boolean isDataFlavorSupported(DataFlavor flavor) {
return FLAVOR.equals(flavor);
}
@Override
public Object getTransferData(DataFlavor flavor)
throws UnsupportedFlavorException, IOException {
if (FLAVOR.equals(flavor)) {
return tree;
}
throw new UnsupportedFlavorException(flavor);
}
}
private static final class TableTransferHandler<T extends Number & Comparable<T>>
extends TransferHandler {
private static final long serialVersionUID = -1794178812912767711L;
@Override
public boolean importData(JComponent comp, Transferable t) {
if (hasTreeFlavor(t.getTransferDataFlavors())) {
try {
final Tree tree = (Tree) t.getTransferData(FLAVOR);
if (comp instanceof JLabel) {
final JLabel label = (JLabel) comp;
label.setText(label.getText() + tree.toString() + ", ");
}
} catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace();
}
}
return false;
}
@Override
public boolean canImport(JComponent comp, DataFlavor[] transferFlavors) {
return hasTreeFlavor(transferFlavors);
}
private boolean hasTreeFlavor(DataFlavor[] transferDataFlavors) {
for (final DataFlavor flavor : transferDataFlavors) {
if (FLAVOR.equals(flavor)) {
return true;
}
}
return false;
}
}
public static void main(String[] args) {
final JFrame frame = new JFrame("Drag And Drop Test");
frame.add(new DnDTest());
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(1000, 800));
frame.pack();
frame.setVisible(true);
}
}
任何想法将不胜感激。