Swing/DnDList のバックアップ差分(No.2)
- バックアップ一覧
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/DnDList へ行く。
- 1 (2006-06-27 (火) 16:34:22)
- 2 (2006-06-29 (木) 15:24:49)
- 3 (2006-10-12 (木) 17:38:24)
- 4 (2007-03-05 (月) 13:39:32)
- 5 (2007-03-06 (火) 03:36:53)
- 6 (2007-03-27 (火) 04:54:17)
- 7 (2007-04-02 (月) 16:24:37)
- 8 (2007-08-16 (木) 17:27:30)
- 9 (2007-09-22 (土) 14:57:27)
- 10 (2007-09-22 (土) 16:16:29)
- 11 (2007-09-27 (木) 20:03:38)
- 12 (2007-09-28 (金) 02:24:23)
- 13 (2007-09-28 (金) 17:50:42)
- 14 (2007-09-28 (金) 19:57:32)
- 15 (2007-11-12 (月) 18:29:54)
- 16 (2009-11-16 (月) 10:48:42)
- 17 (2011-05-05 (木) 08:25:18)
- 18 (2013-04-04 (木) 02:32:23)
- 19 (2013-08-29 (木) 01:12:18)
- 20 (2014-04-25 (金) 18:33:23)
- 21 (2014-09-30 (火) 00:47:46)
- 22 (2014-10-07 (火) 17:42:21)
- 23 (2014-11-08 (土) 01:41:12)
- 24 (2014-11-25 (火) 03:03:31)
- 25 (2015-01-04 (日) 07:20:50)
- 26 (2015-08-21 (金) 18:38:25)
- 27 (2015-11-27 (金) 17:19:43)
- 28 (2016-05-22 (日) 21:20:18)
- 29 (2017-03-28 (火) 19:43:54)
- 30 (2017-04-04 (火) 14:17:08)
- 31 (2017-10-27 (金) 16:26:13)
- 32 (2017-12-01 (金) 18:46:02)
- 33 (2018-11-16 (金) 18:25:04)
- 34 (2020-11-06 (金) 09:18:44)
- 35 (2022-10-20 (木) 21:25:19)
- 追加された行はこの色です。
- 削除された行はこの色です。
TITLE:JListの項目をドラッグ&ドロップ
#navi(../)
*JListの項目をドラッグ&ドロップ [#vdb92e79]
>編集者:[[Terai Atsuhiro>terai]]~
作成日:2004-02-16~
更新日:&lastmod;
#contents
**概要 [#va15d9c9]
JListをドラッグ&ドロップして、項目を入れ替えます。
#screenshot
**サンプルコード [#kadf6356]
private Rectangle2D raCueLine = new Rectangle2D.Float();
private Rectangle2D midArea = new Rectangle2D.Float();
public void dragOver(final DropTargetDragEvent e) {
Graphics2D g2 = (Graphics2D) list1.getGraphics();
int cellHeight = (int) list1.getCellBounds(0,0).getHeight();
int cellWidht = (int) list1.getCellBounds(0,0).getWidth();
boolean flag = false;
for(int i=0;i<=model.getSize();i++) {
midArea.setRect(0, cellHeight*i - (int) (cellHeight/2), cellWidht, cellHeight);
if(midArea.contains(e.getLocation())) {
flag = true;
targetIndex = i;
g2.setColor(lineColor);
}else{
g2.setColor(list1.getBackground());
}
raCueLine.setRect(0, i*cellHeight, cellWidht, 2);
g2.fill(raCueLine);
}
//セルの間にターゲットが無かった場合リストの一番下にラインを引く
if(!flag) {
targetIndex = model.getSize();
raCueLine.setRect(0, targetIndex*cellHeight, cellWidht, 2);
g2.setColor(lineColor);
g2.fill(raCueLine);
}
if(isDragAcceptable(e)) {
e.acceptDrag(e.getDropAction());
}else{
e.rejectDrag();
}
//選択がずれないように…
list1.setSelectedIndex(draggedIndex);
}
public void drop(DropTargetDropEvent e) {
Point point = e.getLocation();
Transferable t = e.getTransferable();
DataFlavor[] f = t.getTransferDataFlavors();
try {
if(isDropAcceptable(e)) {
Component comp = (Component) t.getTransferData(f[0]);
Object str = model.getElementAt(draggedIndex);
if(targetIndex==draggedIndex) {
list1.setSelectedIndex(targetIndex);
}else if(targetIndex<draggedIndex) {
model.removeElementAt(draggedIndex);
model.insertElementAt(str, targetIndex);
list1.setSelectedIndex(targetIndex);
}else{
model.insertElementAt(str, targetIndex);
model.removeElementAt(draggedIndex);
list1.setSelectedIndex(targetIndex-1);
}
e.dropComplete(true);
}else{
e.dropComplete(false);
}
}catch(UnsupportedFlavorException ex) {
e.dropComplete(false);
}catch(IOException ie) {
e.dropComplete(false);
}
e.dropComplete(false);
targetIndex = -1;
list1.repaint();
}
-&jnlp;
-&jar;
-&zip;
**解説 [#na78421d]
ドラッグソースとドラッグターゲットの両方をJList自身に設定して、項目をドラッグ&ドロップしているように見せかけています。
**参考リンク [#qca3b1e2]
-[[How to Use Drag and Drop and Data Transfer>http://java.sun.com/docs/books/tutorial/uiswing/misc/dnd.html]]
-[[DND from a JList with a single gesture>http://forum.java.sun.com/thread.jsp?forum=57&thread=465997]]
-[[Smoother Drag Drop JList JTable>http://forum.java.sun.com/thread.jsp?forum=57&thread=470025]]
**コメント [#m0be05ec]
- 以下を修正しました。 -- [[terai]] &new{2006-04-24 (月) 22:47:06};
--ドラッグで選択されたアイテムがずれにくいように修正
--ドロップしたあとアイテムが選択状態になるように修正
--アイテムの無い場所にドラッグすると一番最後の項目の下にラインを引くように修正(以前は一番下のアイテムの下半分にドラッグした場合のみ、そこに線を引いていた)
- アイテムを複数選択した状態でドラッグするとExceptionが発生する不具合を修正しました。 -- [[terai]] &new{2006-06-27 (火) 16:34:22};
#comment