TITLE:JTableの行をドラッグ&ドロップ

JTableの行をドラッグ&ドロップ

編集者:Terai Atsuhiro
作成日:2007-04-02
更新日:2021-05-19 (水) 05:41:38

概要

JTableの行をドラッグ&ドロップで入れ替えます。

#screenshot

サンプルコード

class DnDTable extends JTable implements DragGestureListener, DragSourceListener, Transferable {
  private static final String NAME = "test";
  private static final Color lineColor = new Color(255,100,100);
  private Rectangle2D targetLine = new Rectangle2D.Float();
  private int draggedIndex = -1;
  private int targetIndex  = -1;
  public DnDTable(TableModel model) {
    super(model);
    DropTarget dropTarget = new DropTarget(this,
      DnDConstants.ACTION_COPY_OR_MOVE, new CDropTargetListener(), true);
    DragSource dragSource = new DragSource();
    dragSource.createDefaultDragGestureRecognizer((Component)this,
      DnDConstants.ACTION_COPY_OR_MOVE, (DragGestureListener)this);
  }
  public void paintComponent(Graphics g) {
    super.paintComponent(g);
    if(targetIndex>=0) {
      Graphics2D g2 = (Graphics2D)g;
      g2.setPaint(lineColor);
      g2.fill(targetLine);
    }
  }
  private void initTargetLine(Point p) {
    Rectangle2D testArea = new Rectangle2D.Float();
    int cellHeight = getRowHeight();
    int lineWidht  = getWidth();
    int lineHeight = 2;
    int modelSize  = getRowCount();
    targetIndex = -1;
    for(int i=0;i<modelSize;i++) {
      testArea.setRect(0, cellHeight*i-(cellHeight/2), lineWidht, cellHeight);
      if(testArea.contains(p)) {
        targetIndex = i;
        targetLine.setRect(0, i*cellHeight, lineWidht, lineHeight);
        break;
      }
    }
    if(targetIndex<0) {
      targetIndex = modelSize;
      targetLine.setRect(0, targetIndex*cellHeight-lineHeight, lineWidht, lineHeight);
    }
  }
  // Interface: DragGestureListener
  public void dragGestureRecognized(DragGestureEvent e) {
    if(getSelectedRowCount()>1) return;
    draggedIndex = rowAtPoint(e.getDragOrigin());
    if(draggedIndex<0) return;
    try{
      e.startDrag(DragSource.DefaultMoveDrop, (Transferable)this, (DragSourceListener)this);
    }catch(InvalidDnDOperationException idoe) {}
  }
  // Interface: DragSourceListener
  public void dragEnter(DragSourceDragEvent e) {
    e.getDragSourceContext().setCursor(DragSource.DefaultMoveDrop);
  }
  public void dragExit(DragSourceEvent e) {
    e.getDragSourceContext().setCursor(DragSource.DefaultMoveNoDrop);
  }
  public void dragOver(DragSourceDragEvent e) {}
  public void dropActionChanged(DragSourceDragEvent e) {}
  public void dragDropEnd(DragSourceDropEvent e) {}
  // Interface: Transferable
  private final DataFlavor FLAVOR
    = new DataFlavor(DataFlavor.javaJVMLocalObjectMimeType, NAME);
  public Object getTransferData(DataFlavor flavor) {
    return this;
  }
  public DataFlavor[] getTransferDataFlavors() {
    DataFlavor[] f = new DataFlavor[1];
    f[0] = this.FLAVOR;
    return f;
  }
  public boolean isDataFlavorSupported(DataFlavor flavor) {
    return flavor.getHumanPresentableName().equals(NAME);
  }
 
  class CDropTargetListener implements DropTargetListener{
    // DropTargetListener interface
    public void dragExit(DropTargetEvent e) {
      targetIndex = -1;
      repaint();
    }
    public void dragEnter(DropTargetDragEvent e) {
      if(isDragAcceptable(e)) {
        e.acceptDrag(e.getDropAction());
      }else{
        e.rejectDrag();
      }
    }
    public void dragOver(final DropTargetDragEvent e) {
      if(isDragAcceptable(e)) {
        e.acceptDrag(e.getDropAction());
      }else{
        e.rejectDrag();
        return;
      }
      initTargetLine(e.getLocation());
      repaint();
    }
    public void dropActionChanged(DropTargetDragEvent e) {
      // if(isDragAcceptable(e)) e.acceptDrag(e.getDropAction());
      // else e.rejectDrag();
    }
    public void drop(DropTargetDropEvent e) {
      DefaultTableModel model = (DefaultTableModel)getModel();
      Transferable t = e.getTransferable();
      DataFlavor[] f = t.getTransferDataFlavors();
      try {
        if(isDropAcceptable(e)) {
          Component comp = (Component) t.getTransferData(f[0]);
          if(targetIndex==draggedIndex) {
            setRowSelectionInterval(targetIndex,targetIndex);
          }else{
            int tg = (targetIndex<draggedIndex)?targetIndex:targetIndex-1;
            model.moveRow(draggedIndex,draggedIndex,tg);
            setRowSelectionInterval(tg,tg);
          }
          e.dropComplete(true);
        }else{
          e.dropComplete(false);
        }
      }catch(UnsupportedFlavorException ex) {
        e.dropComplete(false);
      }catch(IOException ie) {
        e.dropComplete(false);
      }
      e.dropComplete(false);
      targetIndex = -1;
      repaint();
    }
    public boolean isDragAcceptable(DropTargetDragEvent e) {
      DataFlavor[] f = e.getCurrentDataFlavors();
      return isDataFlavorSupported(f[0]);
    }
    public boolean isDropAcceptable(DropTargetDropEvent e) {
      Transferable t = e.getTransferable();
      DataFlavor[] f = t.getTransferDataFlavors();
      return isDataFlavorSupported(f[0]);
    }
  }
}
  • &jnlp;
  • &jar;
  • &zip;

解説

ドラッグソースとドラッグターゲットをどちらもJTableにして、行の入れ替えがドラッグ&ドロップで出来るようになっています。

このサンプルは、行の入れ替え処理を除けば、JListの項目をドラッグ&ドロップとほとんど同一です。

複数アイテムを選択しての移動には対応していません。

参考リンク

コメント