Swing/FileListFlavor のバックアップソース(No.22)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- バックアップ を表示
- Swing/FileListFlavor へ行く。
- 1 (2005-09-13 (火) 00:21:32)
- 2 (2006-01-29 (日) 01:30:22)
- 3 (2006-02-27 (月) 15:54:09)
- 4 (2006-04-16 (日) 01:04:41)
- 5 (2006-11-14 (火) 13:40:54)
- 6 (2007-05-16 (水) 15:03:20)
- 7 (2007-08-21 (火) 13:25:00)
- 8 (2010-12-12 (日) 15:47:24)
- 9 (2010-12-20 (月) 20:15:23)
- 10 (2011-03-08 (火) 18:56:10)
- 11 (2013-03-28 (木) 18:00:24)
- 12 (2013-05-26 (日) 05:40:19)
- 13 (2014-03-24 (月) 18:49:23)
- 14 (2014-12-31 (水) 01:43:34)
- 15 (2015-01-04 (日) 07:17:27)
- 16 (2015-01-21 (水) 18:36:02)
- 17 (2016-08-09 (火) 13:52:20)
- 18 (2016-09-08 (木) 17:55:23)
- 19 (2017-10-20 (金) 14:44:13)
- 20 (2018-09-07 (金) 16:36:33)
- 21 (2020-08-30 (日) 20:09:47)
- 22 (2022-03-06 (日) 07:26:05)
--- category: swing folder: FileListFlavor title: Fileのドラッグ&ドロップ tags: [DragAndDrop, JTable, DropTargetListener, TransferHandler] author: aterai pubdate: 2005-06-20T11:06:07+09:00 description: WindowsなどからファイルをJTableにドラッグ&ドロップします。 image: https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTMhxsIIsI/AAAAAAAAAZU/iZ6Pn8yTFFM/s800/FileListFlavor.png --- * 概要 [#summary] `Windows`などからファイルを`JTable`にドラッグ&ドロップします。 #download(https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTMhxsIIsI/AAAAAAAAAZU/iZ6Pn8yTFFM/s800/FileListFlavor.png) * サンプルコード [#sourcecode] #code(link){{ final FileModel model = new FileModel(); final JTable table = new JTable(model); DropTargetListener dtl = new DropTargetAdapter() { @Override public void dragOver(DropTargetDragEvent dtde) { if (dtde.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) { dtde.acceptDrag(DnDConstants.ACTION_COPY); return; } dtde.rejectDrag(); } @Override public void drop(DropTargetDropEvent dtde) { try { if (dtde.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) { dtde.acceptDrop(DnDConstants.ACTION_COPY); Transferable transferable = dtde.getTransferable(); List list = (List) transferable.getTransferData( DataFlavor.javaFileListFlavor); for (Object o: list) { if (o instanceof File) { File file = (File) o; model.addFileName( new FileName(file.getName(), file.getAbsolutePath())); } } dtde.dropComplete(true); return; } } catch (UnsupportedFlavorException | IOException ex) { ex.printStackTrace(); } dtde.rejectDrop(); } }; new DropTarget(table, DnDConstants.ACTION_COPY, dtl, true); }} * 解説 [#explanation] 上記のサンプルでは、`Windows`のファイルエクスプローラなどからファイルを選択(複数選択可)、ドラッグ、`JTable`上にドロップで、ファイル名などを一覧表示できます。 ドロップされた`DataFlavor`が`DataFlavor.javaFileListFlavor`の場合のみ、これをファイルとして処理する`DropTargetListener`を作成しています。 ---- - `JDK 1.6.0`以降では以下のような`TransferHandler`が使用可能 -- [[JTableでファイルとディレクトリを別々にソート>Swing/FileDirectoryComparator]]に移動 * 参考リンク [#reference] - [http://www5.big.or.jp/~tera/Labo/Java2/j2dnd.html Java2: WindowsからのDrag and Drop] - [http://www.ne.jp/asahi/j.nihei/personal/linuxDragDrop.html java drag and drop] - [[JTable自体の高さを拡張>Swing/FillsViewportHeight]] * コメント [#comment] #comment #comment