gpt4 book ai didi

java - JTable 拖放

转载 作者:行者123 更新时间:2023-11-30 03:08:29 24 4
gpt4 key购买 nike

我正在尝试实现 JTable 拖放,我已经能够根据此问题( How do I drag and drop a row in a JTable? )中提供的解决方案使其大部分工作,并且此问题( Moving rows in JTable )稍微调整为我的需求。

我也遇到的问题是,尽管使用像此处找到的那样的 TableModel ( Moving rows in JTable ),但它在大多数情况下都可以正常工作,例如拖动三行 JTable,将第一行拖动到第二行工作正常,例如将第一行拖动到第三行下方的底部会导致 ArrayIndexOutOfBoundsException 并且该行消失。错误发生在以下行: getDataVector().add(to, o);

来自 Moving rows in JTable 的以下代码会出现此问题:

public class ReorderableTableModel extends DefaultTableModel implements Reorderable {
public void reorder(int from, int to) {
Object o = getDataVector().remove(from);
getDataVector().add(to, o);
fireTableDataChanged();
}
}

亚伦似乎也对这个问题给出了一个答案,解释为什么它可能不起作用: Moving rows in JTable尽管我不确定如何将亚伦的答案应用到我的场景中。

堆栈跟踪是:

java.lang.ArrayIndexOutOfBoundsException: 2 > 1
at java.util.Vector.insertElementAt(Vector.java:598)
at java.util.Vector.add(Vector.java:814)
at com.testproject.test.table.TableModel.reorder(TableModel.java:25)
at com.testproject.test.table.TableRowTransferHandler.importData(TableRowTransferHandler.java:49)
at javax.swing.TransferHandler$DropHandler.drop(TransferHandler.java:1544)
at java.awt.dnd.DropTarget.drop(DropTarget.java:455)
at javax.swing.TransferHandler$SwingDropTarget.drop(TransferHandler.java:1282)
at sun.awt.dnd.SunDropTargetContextPeer.processDropMessage(SunDropTargetContextPeer.java:538)
at sun.lwawt.macosx.CDropTargetContextPeer.processDropMessage(CDropTargetContextPeer.java:143)
at sun.awt.dnd.SunDropTargetContextPeer$EventDispatcher.dispatchDropEvent(SunDropTargetContextPeer.java:852)
at sun.awt.dnd.SunDropTargetContextPeer$EventDispatcher.dispatchEvent(SunDropTargetContextPeer.java:776)
at sun.awt.dnd.SunDropTargetEvent.dispatch(SunDropTargetEvent.java:48)
at java.awt.Component.dispatchEventImpl(Component.java:4746)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4713)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processDropTargetEvent(Container.java:4599)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4461)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2750)
at java.awt.Component.dispatchEvent(Component.java:4713)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

我已尝试解决此问题,但无法解决,解决此问题的最佳方法是什么?

最佳答案

由于您已从 Vector 中删除了一个元素,因此您应该考虑将某些索引变为index-=1

<小时/>

解决方案

public void reorder(int from, int to) {
Object o = getDataVector().remove(from);
getDataVector().add(from>to?to:to-1, o);
fireTableDataChanged();
}

检查from的索引是否大于to。如果是这样,那么我们就知道 to 的索引没有改变,如果没有改变,则通过将索引删除 1 来改变它。

关于java - JTable 拖放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34168104/

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