2

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

Community
  • 1
  • 1
Perroin Thibault
  • 557
  • 1
  • 13
  • 27

2 Answers2

8

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

Sergiy Medvynskyy
  • 11,160
  • 1
  • 32
  • 48
6

The question How do I drag and drop a row in a JTable? contains all the information you need.

When you create the JTable, you have to specify a table model. This model must implement the interface Reorderable (copy the code from the question above) so the TransferHandler will work. This isn't an official Swing API, it's just something that makes the TransferHandler reusable.

The new method must move the row at fromIndex to the new position toIndex. If you use an ArrayList:

 Object row = list.remove( fromIndex );
 if( fromIndex < toIndex ) { toIndex --; } // We now have one row less!
 list.add( toIndex, row );
Community
  • 1
  • 1
Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • Thx, my table model is in my View and declare like this :modeleJTableCIO = new DefaultTableModel(){ @Override public boolean isCellEditable(int row, int column) { return false; } }; How can I implements Reorderable so ? – Perroin Thibault Jan 25 '13 at 15:59
  • @PerroinThibault: move the inline class into a new type/file. Create a new "Reorderable.java" and put the code for the interface in there. Make the new type implement the interface. – Aaron Digulla Jan 25 '13 at 16:04