我正在尝试实现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();
}
}
Aaron在这个问题上似乎也有一个答案,为什么它可能不起作用:Moving rows in JTable虽然我不确定如何在我的场景中实现Aaron的答案。
Stacktrace是:
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)
虽然我无法解决此问题,但是,解决此问题的最佳方法是什么?
答案 0 :(得分:2)
由于您已从Vector
中删除了元素,因此您应该考虑将某些indexes
变为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
的索引没有改变,如果不是,则通过删除索引来改变它。