gpt4 book ai didi

java - DragGestureEvent 的 startDrag 调用后 JTable 的列错位

转载 作者:行者123 更新时间:2023-12-02 06:44:49 26 4
gpt4 key购买 nike

我已经实现了 JTableHeader 的拖放,因此当用户单击表头并按下 Ctrl 键而不是对列重新排序时,将传输自定义对象。

它看起来工作正常,但是当调用 DragGestureEvent 的 startDrag 方法时,被拖动的列变得不合适(请参阅第 4 列和第 5 列之间的间隙):

enter image description here

如果此后单击该列或在不使用 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);
}
}

任何想法将不胜感激。

最佳答案

dragGestureRecognized 的实现中,您可以调用 tableHeader.setDraggedDistance(0);

我对此进行了测试,它对我有用。

关于java - DragGestureEvent 的 startDrag 调用后 JTable 的列错位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18767296/

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