拖放Jtable行

时间:2014-02-17 14:51:04

标签: java swing drag-and-drop

我是Java新手并且正在努力从VB.NET转换我需要重新命令JTable行并且已经关注了这个论坛上的文章: - [如何在JTable中拖放行?

作为java的新手,这一切都超出了我。

我的JTable有一个扩展的 defaulttablemodel 因此: -

       denominationTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
       denominationTable.getTableHeader().setReorderingAllowed(false);
       denominationTable.setAutoCreateRowSorter(true);

       DefaultTableModel denominationReorderableTableModel = new ReorderableTableModel();

       denominationReorderableTableModel.addColumn("Denomination");

       denominationTable.setModel(denominationReorderableTableModel);
       denominationTable.getColumnModel().getColumn(0).setPreferredWidth(100);    
       denominationTable.setDragEnabled(true);
       denominationTable.setDropMode(DropMode.INSERT_ROWS);
       denominationTable.setTransferHandler(new TableRowTransferHandler(denominationTable)); 

ReorderableTableModel如下所示:

  public class ReorderableTableModel extends DefaultTableModel {

            public interface Reorderable {

                   public void reorder(int fromIndex, int toIndex);
        }

        }

TableRowTransferHandler是根据所引用的文章: -

     /**
      * Handles drag & drop row reordering
      */
     public class TableRowTransferHandler extends TransferHandler {
     private final DataFlavor localObjectFlavor = new ActivationDataFlavor(Integer.class    DataFlavor.javaJVMLocalObjectMimeType, "Integer Row Index");
   private JTable           table             = null;

   public TableRowTransferHandler(JTable table) {
      this.table = table;
   }

   @Override
   protected Transferable createTransferable(JComponent c) {
      assert (c == table);
      return new DataHandler(new Integer(table.getSelectedRow()), localObjectFlavor.getMimeType());
   }

   @Override
   public boolean canImport(TransferHandler.TransferSupport info) {
      boolean b = info.getComponent() == table && info.isDrop() && info.isDataFlavorSupported(localObjectFlavor);
      table.setCursor(b ? DragSource.DefaultMoveDrop : DragSource.DefaultMoveNoDrop);
      return b;
   }

   @Override
   public int getSourceActions(JComponent c) {
      return TransferHandler.COPY_OR_MOVE;
   }

   @Override
   public boolean importData(TransferHandler.TransferSupport info) {
      JTable target = (JTable) info.getComponent();
      JTable.DropLocation dl = (JTable.DropLocation) info.getDropLocation();
      int index = dl.getRow();
      int max = table.getModel().getRowCount();
      if (index < 0 || index > max)
         index = max;
      target.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
      try {
         Integer rowFrom = (Integer) info.getTransferable().getTransferData(localObjectFlavor);
         if (rowFrom != -1 && rowFrom != index) {
            ((Reorderable)table.getModel()).reorder(rowFrom, index);
            if (index > rowFrom)
               index--;
            target.getSelectionModel().addSelectionInterval(index, index);
            return true;
         }
      } catch (Exception e) {
         e.printStackTrace();
      }
      return false;
   }

   @Override
   protected void exportDone(JComponent c, Transferable t, int act) {
      if (act == TransferHandler.MOVE) {
         table.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
      }
   }

}

我注意到如果我按照

将方法添加到denominationReorderableTableModel
DefaultTableModel denominationReorderableTableModel = new ReorderableTableModel();

它不可见!既不可重新安装。

Netbeans不会抱怨任何事情。

当我运行它并尝试重新排序我得到的行时: -

java.lang.ClassCastException: mycoins.ReorderableTableModel cannot be cast to mycoins.ReorderableTableModel$Reorderable
at mycoins.TableRowTransferHandler.importData(TableRowTransferHandler.java:64)
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.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:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
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:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

两天的研究,我没有进一步前进。我找不到任何能给我一些线索的参考资料。

我甚至试过一个抽象模型 - 结果相同。

这可能是一个新手愚蠢的错误。

请您的想法/意见。

2 个答案:

答案 0 :(得分:0)

阅读Drag and Drop上Swing教程中的部分,了解DnD如何工作的基础知识。基本上,您需要创建一个支持将数据放入TableModel的TransferHandler。

以上是上一版教程中包含的旧ExtendedDnDDemo代码的链接。它包括一个JTable的TransferHandler。

答案 1 :(得分:0)

我有另一个来源的工作示例。(camickr)

但它与我尝试的非常不同。

原作的作者已经重新发送个人电子邮件: -

您需要使自定义表模型成为DefaultTableModel的子类,然后您需要声明它以实现我的“可重新排序”接口。我建议参加官方的java教程(参见sun / oracle java网站)并阅读类继承和接口。

感谢所有的输入 - 我显然有很多东西可以学习Java