protected void processDrag(DropTargetDragEvent dtde) { if (dtde.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) { dtde.acceptDrag(DnDConstants.ACTION_COPY); } else { dtde.rejectDrag(); } }
/** * Creates a droppable text pane. */ public DroppableTextPane() { new DropTarget(this, this); dragSource.createDefaultDragGestureRecognizer(this, DnDConstants.ACTION_COPY_OR_MOVE, this); }
@Override public void dragEnter(DropTargetDragEvent dropTargetDragEvent) { dropTargetDragEvent.acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE); }
@Override @SuppressWarnings("unchecked") public void drop(DropTargetDropEvent dtde) { if (!dtde.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) { dtde.rejectDrop(); return; } dtde.acceptDrop(dtde.getDropAction()); try { Transferable transferable = dtde.getTransferable(); List<File> transferData = (List<File>) transferable.getTransferData(DataFlavor.javaFileListFlavor); if (!transferData.isEmpty()) { dtde.dropComplete(true); // load first file mainWindow.openFile(transferData.get(0)); } } catch (Exception e) { LOG.error("File drop operation failed", e); } }
@Override public void drop(DropTargetDropEvent dropTargetDropEvent) { try { Transferable transferable = dropTargetDropEvent.getTransferable(); if (transferable.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) { dropTargetDropEvent.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE); List fileList = (List) transferable.getTransferData(DataFlavor.javaFileListFlavor); Iterator iterator = fileList.iterator(); while (iterator.hasNext()) { File file = (File) iterator.next(); if (file.isFile()) { fileDropped(file); } if (file.isDirectory()) { directoryDropped(file); } } dropTargetDropEvent.getDropTargetContext().dropComplete(true); } else { dropTargetDropEvent.rejectDrop(); } } catch (IOException | UnsupportedFlavorException io) { io.printStackTrace(); dropTargetDropEvent.rejectDrop(); } }
@Override public void drop(DropTargetDropEvent evt) { final int action = evt.getDropAction(); evt.acceptDrop(action); try { Transferable data = evt.getTransferable(); if (data.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) { java.util.List<File> list = (java.util.List<File>) data.getTransferData( DataFlavor.javaFileListFlavor); for(DndAction a: actions) { a.onDrop(list); } } } catch (UnsupportedFlavorException | IOException e) { LOG.log(Level.WARNING, null, e); } finally { evt.dropComplete(true); evt.getDropTargetContext().getComponent() .setCursor(Cursor.getDefaultCursor()); } }
JTextArea myPanel = new JTextArea(); myPanel.setDropTarget(new DropTarget() { public synchronized void drop(DropTargetDropEvent evt) { try { evt.acceptDrop(DnDConstants.ACTION_COPY); List<File> droppedFiles = (List<File>) evt.getTransferable().getTransferData(DataFlavor.javaFileListFlavor); for (File file : droppedFiles) { // process files } } catch (Exception ex) { ex.printStackTrace(); } } });
@Override public void dropActionChanged( java.awt.dnd.DropTargetDragEvent evt) { log(out, "FileDrop: dropActionChanged event."); // Is this an acceptable drag event? if (FileDrop.this.isDragOk(out, evt)) { // evt.acceptDrag( // java.awt.dnd.DnDConstants.ACTION_COPY_OR_MOVE // ); evt.acceptDrag(java.awt.dnd.DnDConstants.ACTION_COPY); log(out, "FileDrop: event accepted."); } // end if: drag ok else { evt.rejectDrag(); log(out, "FileDrop: event rejected."); } // end else: drag not ok } // end dropActionChanged }; // end DropTargetListener
@Override public void hierarchyChanged(java.awt.event.HierarchyEvent evt) { log(out, "FileDrop: Hierarchy changed."); java.awt.Component parent = c.getParent(); if (parent == null) { c.setDropTarget(null); log(out, "FileDrop: Drop target cleared from component."); } // end if: null parent else { new java.awt.dnd.DropTarget(c, FileDrop.this.dropListener); log(out, "FileDrop: Drop target added to component."); } // end else: parent not null } // end hierarchyChanged }); // end hierarchy listener
@Override public void dragExit(DropTargetEvent dte) { dte.getDropTargetContext().getComponent() .setCursor(Cursor.getDefaultCursor()); }
@Override public void dragEnter(DropTargetDragEvent dtde) { dtde.getDropTargetContext().getComponent() .setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR)); }
@Override public void drop(DropTargetDropEvent dropTargetDropEvent) { try { Transferable transferable = dropTargetDropEvent.getTransferable(); if (transferable.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) { dropTargetDropEvent.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE); List fileList = (List) transferable.getTransferData(DataFlavor.javaFileListFlavor); Iterator iterator = fileList.iterator(); while (iterator.hasNext()) { File file = (File) iterator.next(); if (file.isFile()) { fileDropped(file); } if (file.isDirectory()) { directoryDropped(file); } } dropTargetDropEvent.getDropTargetContext().dropComplete(true); } else { dropTargetDropEvent.rejectDrop(); } } catch (IOException | UnsupportedFlavorException io) { io.printStackTrace(); dropTargetDropEvent.rejectDrop(); } }
@Override public void dragEnter(DropTargetDragEvent event) { if (event.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) { event.acceptDrag(DnDConstants.ACTION_COPY); } else { event.rejectDrag(); } }
/** * Creates a droppable rame. */ public DroppableFrame() { new DropTarget(this, this); dragSource.createDefaultDragGestureRecognizer(this, DnDConstants.ACTION_COPY_OR_MOVE, this); }
@Override public void dragEnter(java.awt.dnd.DropTargetDragEvent evt) { log(out, "FileDrop: dragEnter event."); // Is this an acceptable drag event? if (FileDrop.this.isDragOk(out, evt)) { // If it's a Swing component, set its border if (c instanceof javax.swing.JComponent) { javax.swing.JComponent jc = (javax.swing.JComponent) c; FileDrop.this.normalBorder = jc.getBorder(); log(out, "FileDrop: normal border saved."); jc.setBorder(dragBorder); log(out, "FileDrop: drag border set."); } // end if: JComponent if (listener != null) { listener.dragEnter(c); } // Acknowledge that it's okay to enter // evt.acceptDrag( // java.awt.dnd.DnDConstants.ACTION_COPY_OR_MOVE ); evt.acceptDrag(java.awt.dnd.DnDConstants.ACTION_COPY); log(out, "FileDrop: event accepted."); } // end if: drag ok else { // Reject the drag event evt.rejectDrag(); log(out, "FileDrop: event rejected."); } // end else: drag not ok } // end dragEnter
private void dnd() { FileDropTargetListener l = new FileDropTargetListener(); l.addDndAction(new DndAction() { @Override public void onDrop(List<File> files) { loadFromFile(files.get(0)); } }); new DropTarget(jta_in, l); }
@Override public void dragEnter(DropTargetDragEvent dropTargetDragEvent) { dropTargetDragEvent.acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE); }
@Override public void dragOver(DropTargetDragEvent dtde) { dtde.getDropTargetContext().getComponent() .setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR)); }
private JDesktopPane initDesktopPane(GCViewerGui gui, GCModelLoaderController controller) { JDesktopPane desktopPane = new DesktopPane(); desktopPane.setDropTarget(new DropTarget(gui, DnDConstants.ACTION_COPY, new FileDropTargetListener(controller, DropFlavor.OPEN))); return desktopPane; }