Swing/DnDList のバックアップの現在との差分(No.25)
- category: swing folder: DnDList title: JListの項目をドラッグ&ドロップ tags: [JList, DragAndDrop] author: aterai pubdate: 2004-02-16 description: JListをドラッグ&ドロップして、項目を入れ替えます。 description: JListをドラッグ&ドロップして項目を入れ替えます。 image:
概要
JList
をドラッグ&ドロップして、項目を入れ替えます。
概要
JList
をドラッグ&ドロップして項目を入れ替えます。
Screenshot
Advertisement
サンプルコード
サンプルコード
@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
自身に設定して、項目をドラッグ&ドロップしているように見せかけています。
- メモ:
- 複数アイテムを選択して移動する場合は、TransferHandlerを使ったJListのドラッグ&ドロップによる並べ替えのサンプルなどを参照
- 複数アイテムを選択して移動する場合はTransferHandlerを使ったJListのドラッグ&ドロップによる並べ替えのサンプルなどを参照
参考リンク
参考リンク
- Introduction to Drag and Drop and Data Transfer
- Swing (Archive) - DND from a JList with a single gesture
- Swing (Archive) - Smoother Drag Drop JList JTable
- TransferHandlerを使ったJListのドラッグ&ドロップによる並べ替え
- JListのアイテムをラバーバンドで複数選択、ドラッグ&ドロップで並べ替え