本文介绍了在JTable中移动行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用MVC模式,并且有一个像这样的JTable:

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();

我想在此JTable中使用拖放行,因此我实现了以下代码:

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?

但是我不明白如何为TableModel实现可重排序"接口.

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)

55行是:

((Reorderable)table.getModel()).reorder(rowFrom, index);

有人可以帮助我,并解释它的工作原理吗?

Someone can help me, and explain how it's work ?

谢谢

推荐答案

如果我正确理解了示例中用于实现接口Reorderable的代码,则只需将行放在fromIndex处并插入到toIndex.

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.

示例:

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

别忘了将此表模型的instanceo设置为表

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

这篇关于在JTable中移动行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 21:10