maldahleh
maldahleh

Reputation: 313

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.

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. The error occurs in the line: getDataVector().add(to, o);

The issue occurs with the following code from 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();
    }
}

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.

Stacktrace is:

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?

Upvotes: 1

Views: 314

Answers (1)

Yassin Hajaj
Yassin Hajaj

Reputation: 21975

Since you've removed an element from the Vector you should consider some of your indexes to become 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. 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.

Upvotes: 2

Related Questions