Swing/DnDReorderList のバックアップ差分(No.19)
- バックアップ一覧
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/DnDReorderList へ行く。
- 1 (2008-09-29 (月) 13:33:14)
- 2 (2008-09-29 (月) 19:17:20)
- 3 (2008-10-10 (金) 21:43:01)
- 4 (2008-10-14 (火) 12:40:16)
- 5 (2009-03-16 (月) 17:07:12)
- 6 (2009-09-26 (土) 02:17:57)
- 7 (2010-08-19 (木) 02:06:33)
- 8 (2010-10-27 (水) 02:30:10)
- 9 (2010-12-20 (月) 21:43:52)
- 10 (2012-07-22 (日) 23:45:57)
- 11 (2012-09-25 (火) 18:07:57)
- 12 (2013-01-17 (木) 15:14:21)
- 13 (2014-09-30 (火) 00:52:20)
- 14 (2015-01-01 (木) 21:44:05)
- 15 (2015-02-20 (金) 13:19:19)
- 16 (2015-03-29 (日) 15:21:09)
- 17 (2017-02-21 (火) 16:40:12)
- 18 (2017-04-04 (火) 14:17:08)
- 19 (2017-11-17 (金) 17:45:46)
- 20 (2018-02-14 (水) 17:50:37)
- 21 (2019-09-26 (木) 20:50:42)
- 22 (2019-10-23 (水) 21:27:14)
- 23 (2021-05-25 (火) 08:28:48)
- 追加された行はこの色です。
- 削除された行はこの色です。
--- category: swing folder: DnDReorderList title: TransferHandlerを使ったJListのドラッグ&ドロップによる並べ替え tags: [JList, TransferHandler, DragAndDrop] author: aterai pubdate: 2008-09-29T13:33:14+09:00 description: JListのアイテムを複数選択し、ドラッグ&ドロップで並べ替えを可能にするTransferHandlerを作成します。 image: https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTLeSCyHuI/AAAAAAAAAXo/v2OLiSPdgEY/s800/DnDReorderList.png --- * 概要 [#summary] `JList`のアイテムを複数選択し、ドラッグ&ドロップで並べ替えを可能にする`TransferHandler`を作成します。 #download(https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTLeSCyHuI/AAAAAAAAAXo/v2OLiSPdgEY/s800/DnDReorderList.png) * サンプルコード [#sourcecode] #code(link){{ class ListItemTransferHandler extends TransferHandler { private final DataFlavor localObjectFlavor; private Object[] transferedObjects = null; public ListItemTransferHandler() { localObjectFlavor = new ActivationDataFlavor( Object[].class, DataFlavor.javaJVMLocalObjectMimeType, "Array of items"); } @Override protected Transferable createTransferable(JComponent c) { JList list = (JList) c; indices = list.getSelectedIndices(); transferedObjects = list.getSelectedValues(); return new DataHandler(transferedObjects, localObjectFlavor.getMimeType()); } @Override public boolean canImport(TransferSupport info) { if (!info.isDrop() || !info.isDataFlavorSupported(localObjectFlavor)) { return false; } return true; } @Override public int getSourceActions(JComponent c) { return TransferHandler.MOVE; //TransferHandler.COPY_OR_MOVE; } @Override public boolean importData(TransferSupport info) { if (!canImport(info)) { return false; } JList target = (JList) info.getComponent(); JList.DropLocation dl = (JList.DropLocation) info.getDropLocation(); DefaultListModel listModel = (DefaultListModel) target.getModel(); int index = dl.getIndex(); //boolean insert = dl.isInsert(); int max = listModel.getSize(); if (index < 0 || index > max) { index = max; } addIndex = index; try { Object[] values = (Object[]) info.getTransferable().getTransferData(localObjectFlavor); addCount = values.length; for (int i = 0; i < values.length; i++) { int idx = index++; listModel.add(idx, values[i]); target.addSelectionInterval(idx, idx); } return true; } catch (UnsupportedFlavorException | IOException ex) { ex.printStackTrace(); } return false; } @Override protected void exportDone( JComponent c, Transferable data, int action) { cleanup(c, action == TransferHandler.MOVE); } private void cleanup(JComponent c, boolean remove) { if (remove && indices != null) { JList source = (JList) c; DefaultListModel model = (DefaultListModel) source.getModel(); if (addCount > 0) { for (int i = 0; i < indices.length; i++) { if (indices[i] >= addIndex) { indices[i] += addCount; } } } for (int i = indices.length - 1; i >= 0; i--) { model.remove(indices[i]); } } indices = null; addCount = 0; addIndex = -1; } private int[] indices = null; private int addIndex = -1; //Location where items were added private int addCount = 0; //Number of items added. } }} * 解説 [#explanation] 上記のサンプルの`TransferHandler`は、主に[http://docs.oracle.com/javase/tutorial/uiswing/examples/dnd/index.html#BasicDnD Drag and Drop and Data Transfer: Examples (The Java™ Tutorials > Creating a GUI with JFC/Swing > Drag and Drop and Data Transfer)]の[http://docs.oracle.com/javase/tutorial/uiswing/examples/dnd/DropDemoProject/src/dnd/ListTransferHandler.java ListTransferHandler.java]を参考にして作成しています。ただし、この`ListTransferHandler.java`は項目を複数選択して、`JList`内での並べ替えは想定していない(もしくはバグ?)ようなので、`importData(...)`メソッドや、`cleanup()`メソッドを修正しています。 上記のサンプルの`TransferHandler`は、主に[https://docs.oracle.com/javase/tutorial/uiswing/examples/dnd/index.html#BasicDnD Drag and Drop and Data Transfer: Examples (The Java™ Tutorials > Creating a GUI with JFC/Swing > Drag and Drop and Data Transfer)]の[https://docs.oracle.com/javase/tutorial/uiswing/examples/dnd/DropDemoProject/src/dnd/ListTransferHandler.java ListTransferHandler.java]を参考にして作成しています。ただし、この`ListTransferHandler.java`は項目を複数選択して、`JList`内での並べ替えは想定していない(もしくはバグ?)ようなので、`importData(...)`メソッドや、`cleanup()`メソッドを修正しています。 #code{{ JList list = new JList(listModel); list.getSelectionModel().setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); list.setTransferHandler(new ListItemTransferHandler()); list.setDropMode(DropMode.INSERT); list.setDragEnabled(true); }} - `importData` -- 使用されていない?`importString(...)`の内容をこちらに移動 - `cleanup` -- 例えば、項目`0`,`1`,`2`を複数選択して、`1`と`2`の間にドロップすると、`1`,`2`,`2`になるので、以下のように修正 #code{{ for (int i = 0; i < indices.length; i++) { //if (indices[i] > addIndex) { if (indices[i] >= addIndex) { //... }} ---- [[JListの項目をドラッグ&ドロップ>Swing/DnDList]]とは異なり、複数アイテムを選択して`Drag&Drop`による移動が可能になっています。 * 参考リンク [#reference] - [http://docs.oracle.com/javase/tutorial/uiswing/examples/dnd/index.html#BasicDnD Drag and Drop and Data Transfer: Examples (The Java™ Tutorials > Creating a GUI with JFC/Swing > Drag and Drop and Data Transfer)] -- [http://docs.oracle.com/javase/tutorial/uiswing/examples/dnd/DropDemoProject/src/dnd/ListTransferHandler.java ListTransferHandler.java] - [https://docs.oracle.com/javase/tutorial/uiswing/examples/dnd/index.html#BasicDnD Drag and Drop and Data Transfer: Examples (The Java™ Tutorials > Creating a GUI with JFC/Swing > Drag and Drop and Data Transfer)] -- [https://docs.oracle.com/javase/tutorial/uiswing/examples/dnd/DropDemoProject/src/dnd/ListTransferHandler.java ListTransferHandler.java] - [[JListの項目をドラッグ&ドロップ>Swing/DnDList]] - [[JListのアイテムをラバーバンドで複数選択、ドラッグ&ドロップで並べ替え>Swing/DragSelectDropReordering]] * コメント [#comment] #comment - 複数選択して選択されたアイテムのインデックスに移動した場合、複写されるバグ?を修正。 -- &user(aterai); &new{2008-10-10 (金) 21:44:34}; #comment