• category: swing folder: DnDList title: JListの項目をドラッグ&ドロップ tags: [JList, DragAndDrop] author: aterai pubdate: 2004-02-16 description: JListをドラッグ&ドロップして、項目を入れ替えます。 description: JListをドラッグ&ドロップして項目を入れ替えます。 image: https://lh6.googleusercontent.com/_9Z4BYR88imo/TQTLb3DW2ZI/AAAAAAAAAXk/8VfeirUfaoo/s800/DnDList.png

概要

JListをドラッグ&ドロップして、項目を入れ替えます。

概要

JListをドラッグ&ドロップして項目を入れ替えます。

サンプルコード

サンプルコード

@Override protected void paintComponent(Graphics g) {
  super.paintComponent(g);
  if(targetIndex >= 0) {
    Graphics2D g2 = (Graphics2D) g;
  if (targetIndex >= 0) {
    Graphics2D g2 = (Graphics2D) g.create();
    g2.setPaint(lineColor);
    g2.fill(targetLine);
    g2.dispose();
  }
}
#spanadd

#spanend
private void initTargetLine(Point p) {
  Rectangle2D testArea = new Rectangle2D.Float();
  int cellHeight = (int) getCellBounds(0, 0).getHeight();
  int lineWidht  = (int) getCellBounds(0, 0).getWidth();
  Rectangle rect = getCellBounds(0, 0);
  int cellHeight = rect.height;
  int lineHeight = 2;
  int modelSize  = getModel().getSize();
  targetIndex = -1;
  for(int i = 0; i < modelSize; i++) {
    testArea.setRect(0, cellHeight * i - (cellHeight / 2), lineWidht, cellHeight);
    if(testArea.contains(p)) {
  targetLine.setSize(rect.width, lineHeight);
  for (int i = 0; i < modelSize; i++) {
    rect.setLocation(0, cellHeight * i - cellHeight / 2);
    if (rect.contains(p)) {
      targetIndex = i;
      targetLine.setRect(0, i * cellHeight, lineWidht, lineHeight);
      targetLine.setLocation(0, i * cellHeight);
      break;
    }
  }
  if(targetIndex < 0) {
  if (targetIndex < 0) {
    targetIndex = modelSize;
    targetLine.setRect(0, targetIndex * cellHeight - lineHeight, lineWidht, lineHeight);
    targetLine.setLocation(0, targetIndex * cellHeight - lineHeight);
  }
}
#spanadd

#spanend
@Override public void dragOver(final DropTargetDragEvent e) {
  if(isDragAcceptable(e)) {
  if (isDragAcceptable(e)) {
    e.acceptDrag(e.getDropAction());
  }else{
  } else {
    e.rejectDrag();
    return;
  }
  initTargetLine(e.getLocation());
  repaint();
}
#spanadd

#spanend
@Override public void drop(DropTargetDropEvent e) {
  DefaultListModel model = (DefaultListModel) getModel();
  Transferable t = e.getTransferable();
  DataFlavor[] f = t.getTransferDataFlavors();
  try {
    if(isDropAcceptable(e)) {
    if (isDropAcceptable(e)) {
      Component comp = (Component) t.getTransferData(f[0]);
      Object str = model.getElementAt(draggedIndex);
      if(targetIndex==draggedIndex) {
      if (targetIndex == draggedIndex) {
        setSelectedIndex(targetIndex);
      }else if(targetIndex < draggedIndex) {
      } else if (targetIndex < draggedIndex) {
        model.removeElementAt(draggedIndex);
        model.insertElementAt(str, targetIndex);
        setSelectedIndex(targetIndex);
      }else{
      } else {
        model.insertElementAt(str, targetIndex);
        model.removeElementAt(draggedIndex);
        setSelectedIndex(targetIndex - 1);
      }
      e.dropComplete(true);
    }else{
    } else {
      e.dropComplete(false);
    }
  }catch(UnsupportedFlavorException ex) {
  } catch (UnsupportedFlavorException | IOException ex) {
    e.dropComplete(false);
  }catch(IOException ie) {
    e.dropComplete(false);
  }
  e.dropComplete(false);
  targetIndex = -1;
  repaint();
}
View in GitHub: Java, Kotlin

解説

解説

上記のサンプルでは、ドラッグソースとドラッグターゲットの両方をJList自身に設定して、項目をドラッグ&ドロップしているように見せかけています。

参考リンク

参考リンク

コメント

コメント