Moving rows in JTable

java, jtable, swing

Solution

If I correctly understand the code in the sample to implement the interface `Reorderable` you simply should get the row which is placed at the `fromIndex` and inserts at the `toIndex`.

Example:

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

Do not forget to set the instanceo of this table model to your table

Problem

I work with the MVC pattern and I've a JTable like this : ``` List<Enregistrement> desEnregistrements = dao.lireTousLesEnregistrements(1); ((VueAfficherCompteCIO)vue).getModeleJTableCIO().addColumn("Date Enregistrement"); ((VueAfficherCompteCIO)vue).getModeleJTableCIO().addColumn("Libellé"); ((VueAfficherCompteCIO)vue).getModeleJTableCIO().addColumn("Motif"); ((VueAfficherCompteCIO)vue).getModeleJTableCIO().addColumn("Date facture"); ((VueAfficherCompteCIO)vue).getModeleJTableCIO().addColumn("Mode de règlement"); ((VueAfficherCompteCIO)vue).getModeleJTableCIO().addColumn("Montant"); ((VueAfficherCompteCIO)vue).getModeleJTableCIO().addColumn("Solde"); ((VueAfficherCompteCIO)vue).getModeleJTableCIO().addColumn("Etat"); ((VueAfficherCompteCIO)vue).getModeleJTableCIO().addColumn("Modifier"); ((VueAfficherCompteCIO)vue).getModeleJTableCIO().addColumn("RecDep"); ((VueAfficherCompteCIO)vue).getModeleJTableCIO().addColumn("Anticipation"); for (Enregistrement unEnregistrement : desEnregistrements) { ((VueAfficherCompteCIO)vue).getModeleJTableCIO().addRow(new String[]{unEnregistrement.getDate(), unEnregistrement.getIdLibelle(), unEnregistrement.getMotif(), unEnregistrement.getDateFacture(), unEnregistrement.getModeReglement(), unEnregistrement.getMontant(), unEnregistrement.getNouveauSolde(), unEnregistrement.getIdEtat(),unEnregistrement.getId(), unEnregistrement.getRecetteDepense(), unEnregistrement.getAnticipation()}); } aspectJtable(); ``` I would like to use a drag and move rows with this JTable so I implements this code : How do I drag and drop a row in a JTable? But I don't understand how i can implement the interface "Reorderable" to my TableModel. When I run my project I can use the drag and move (I see the drag...) but there is no movement. Next, I've this error : ``` java.lang.ClassCastException: vues.VueAfficherCompteCIO$1 cannot be cast to controleurs.Reorderable at controleurs.TableRowTransferHandler.importData(TableRowTransferHandler.java:55) at javax.swing.TransferHandler$DropHandler.drop(TransferHandler.java:1536) at java.awt.dnd.DropTarget.drop(DropTarget.java:450) at javax.swing.TransferHandler$SwingDropTarget.drop(TransferHandler.java:1274) at sun.awt.dnd.SunDropTargetContextPeer.processDropMessage(SunDropTargetContextPeer.java:537) at sun.awt.X11.XDropTargetContextPeer.processDropMessage(XDropTargetContextPeer.java:184) at sun.awt.dnd.SunDropTargetContextPeer$EventDispatcher.dispatchDropEvent(SunDropTargetContextPeer.java:851) at sun.awt.dnd.SunDropTargetContextPeer$EventDispatcher.dispatchEvent(SunDropTargetContextPeer.java:775) at sun.awt.dnd.SunDropTargetEvent.dispatch(SunDropTargetEvent.java:48) at java.awt.Component.dispatchEventImpl(Component.java:4716) at java.awt.Container.dispatchEventImpl(Container.java:2287) at java.awt.Component.dispatchEvent(Component.java:4687) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832) at java.awt.LightweightDispatcher.processDropTargetEvent(Container.java:4566) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4417) at java.awt.Container.dispatchEventImpl(Container.java:2273) at java.awt.Window.dispatchEventImpl(Window.java:2719) at java.awt.Component.dispatchEvent(Component.java:4687) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:723) at java.awt.EventQueue.access$200(EventQueue.java:103) at java.awt.EventQueue$3.run(EventQueue.java:682) at java.awt.EventQueue$3.run(EventQueue.java:680) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87) at java.awt.EventQueue$4.run(EventQueue.java:696) at java.awt.EventQueue$4.run(EventQueue.java:694) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:693) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139) at java.awt.EventDispatchThread.run(EventDispatchThread.java:97) ``` The 55 line is : ``` ((Reorderable)table.getModel()).reorder(rowFrom, index); ``` Someone can help me, and explain how it's work ? Thank you

Original source

Related problems