简体   繁体   English

JTable拖放

[英]JTable drag and drop

I am trying to implement JTable drag and drop, I have been able to get it working for the most part based on the solution provided in this question ( How do I drag and drop a row in a JTable? ) and this question ( Moving rows in JTable ) tweaked slightly to my needs. 我正在尝试实现JTable拖放,基于此问题( 如何在JTable中拖放行? )和此问题( 移动行 ),我已经能够使其在大多数情况下都能正常工作在JTable中 )略微调整了我的需求。

The issue I am running into too is it works fine for the most part though using the TableModel like the one found here ( Moving rows in JTable ), dragging in for example a three row JTable, dragging the first row to the second row works fine though dragging for example the first row to the bottom below the third row results in a ArrayIndexOutOfBoundsException and the row disappears. 我也遇到的问题是,尽管使用TableModel像在这里找到的那​​样( 在JTable中移动行 ),例如拖入三行JTable,将第一行拖到第二行,但在大多数情况下都可以正常工作尽管将例如第一行拖到第三行下方的底部会导致ArrayIndexOutOfBoundsException,并且该行消失。 The error occurs in the line: getDataVector().add(to, o); 该错误发生在以下行:getDataVector()。add(to,o);

The issue occurs with the following code from Moving rows in JTable : 使用“ 在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();
    }
}

There seems to also be an answer by Aaron on this question as to why it may not be working: Moving rows in JTable though I was unsure of how to implement Aaron's answer into my scenario. 关于为什么它可能行不通的问题,Aaron似乎也有一个答案:尽管我不确定如何在我的方案中实现Aaron的答案,但在JTable中移动行

Stacktrace is: 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)

I have attempted to resolve this issue though I am unable to, what would be the best way to resolving this issue? 我无法解决此问题,但是解决此问题的最佳方法是什么?

Since you've removed an element from the Vector you should consider some of your indexes to become index-=1 ; 由于已从Vector删除了一个元素,因此应考虑将某些indexes变为index-=1


Solution

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

Check if the index of from is greater than to . 检查from的索引是否大于to的索引。 If it is the case, than we know the index of to has not changed, if it is not, change it by removing one to the index. 如果是这样,则我们知道to的索引没有更改,如果没有更改,请通过删除索引中的一个来更改它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM