Example usage for javafx.scene.input DragEvent getDragboard

List of usage examples for javafx.scene.input DragEvent getDragboard

Introduction

In this page you can find the example usage for javafx.scene.input DragEvent getDragboard.

Prototype

public final Dragboard getDragboard() 

Source Link

Document

A dragboard that is available to transfer data.

Usage

From source file:Main.java

@Override
public void start(Stage primaryStage) {
    Group root = new Group();
    Scene scene = new Scene(root, 551, 400);
    scene.setOnDragOver(new EventHandler<DragEvent>() {
        @Override//  w  w w .  ja  v  a  2  s.c  o  m
        public void handle(DragEvent event) {
            Dragboard db = event.getDragboard();
            if (db.hasFiles()) {
                event.acceptTransferModes(TransferMode.COPY);
            } else {
                event.consume();
            }
        }
    });

    // Dropping over surface
    scene.setOnDragDropped(new EventHandler<DragEvent>() {
        @Override
        public void handle(DragEvent event) {
            Dragboard db = event.getDragboard();
            boolean success = false;
            if (db.hasFiles()) {
                success = true;
                String filePath = null;
                for (File file : db.getFiles()) {
                    filePath = file.getAbsolutePath();
                    System.out.println(filePath);
                }
            }
            event.setDropCompleted(success);
            event.consume();
        }
    });

    primaryStage.setScene(scene);
    primaryStage.show();
}

From source file:at.ac.tuwien.qse.sepm.gui.controller.impl.GridViewImpl.java

private void handleDragDropped(DragEvent event) {
    LOGGER.debug("drag dropped");

    Dragboard dragboard = event.getDragboard();
    boolean success = dragboard.hasFiles();
    if (success) {
        dragboard.getFiles().forEach(f -> LOGGER.debug("dropped file {}", f));
        for (File file : dragboard.getFiles()) {
            try {
                workspaceService.addDirectory(file.toPath());
            } catch (ServiceException ex) {
                LOGGER.error("Couldn't add directory {}");
            }//from w w  w . j av a  2 s  . co m
        }
    }
    event.setDropCompleted(success);
    event.consume();
}

From source file:com.loop81.fxcomparer.FXComparerController.java

/** Enabled drag & drop support on the anchor-panels for the file selections. */
private void initiateDragNDropp() {
    anchorPaneDropFile1.setOnDragOver(new EventHandler<DragEvent>() {
        @Override/* w w  w . ja  v a  2  s . com*/
        public void handle(DragEvent event) {
            if (event.getDragboard().hasFiles()
                    && comparer.isFileSupported(event.getDragboard().getFiles().get(0))) {
                event.acceptTransferModes(TransferMode.COPY);
            } else {
                event.consume();
            }
        }
    });

    anchorPaneDropFile1.setOnDragDropped(new EventHandler<DragEvent>() {
        @Override
        public void handle(DragEvent event) {
            if (event.getDragboard().hasFiles()
                    && comparer.isFileSupported(event.getDragboard().getFiles().get(0))) {
                try {
                    archive1 = new ComparableArchive(event.getDragboard().getFiles().get(0));
                    handleFile(archive1, textFieldArchive1, labelArchive1Info);
                } catch (ArchiveException e) {
                    new AlertDialog(MessageBundle.getString("exceptions.archive.open")).show();
                }
            }
        }
    });

    anchorPaneDropFile2.setOnDragOver(new EventHandler<DragEvent>() {
        @Override
        public void handle(DragEvent event) {
            if (event.getDragboard().hasFiles()
                    && comparer.isFileSupported(event.getDragboard().getFiles().get(0))) {
                event.acceptTransferModes(TransferMode.COPY);
            } else {
                event.consume();
            }
        }
    });

    anchorPaneDropFile2.setOnDragDropped(new EventHandler<DragEvent>() {
        @Override
        public void handle(DragEvent event) {
            if (event.getDragboard().hasFiles()
                    && comparer.isFileSupported(event.getDragboard().getFiles().get(0))) {
                try {
                    archive2 = new ComparableArchive(event.getDragboard().getFiles().get(0));
                    handleFile(archive2, textFieldArchive2, labelArchive2Info);
                } catch (ArchiveException e) {
                    new AlertDialog(MessageBundle.getString("exceptions.archive.open")).show();
                }
            }
        }
    });
}

From source file:com.heliosdecompiler.helios.gui.controller.FileTreeController.java

@FXML
public void startDrop(DragEvent event) {
    Dragboard db = event.getDragboard();
    if (db.hasFiles()) {
        event.acceptTransferModes(TransferMode.ANY);
    }//www  .j  a v a 2 s  . c om
}

From source file:net.rptools.tokentool.controller.ManageOverlays_Controller.java

@FXML
void overlayViewFlowPane_DragOver(DragEvent event) {
    if (event.getDragboard().hasImage() || event.getDragboard().hasFiles() || event.getDragboard().hasUrl()) {
        // TODO: Set Pane color to an alpha green
        event.acceptTransferModes(TransferMode.COPY);
    } else {/*from w w w  .  ja va  2 s  .  c  o m*/
        // TODO: Set Pane color to an alpha red?
        event.acceptTransferModes(TransferMode.ANY);
    }
}

From source file:com.heliosdecompiler.helios.gui.controller.FileTreeController.java

@FXML
public void stopDrop(DragEvent event) {
    Dragboard db = event.getDragboard();
    boolean success = false;
    if (db.hasFiles()) {
        success = true;/*from w  w w . j  a va2s  .co  m*/
        for (File file : db.getFiles()) {
            if (file.getName().endsWith(".jar") || file.getName().endsWith(".class")) {
                openedFileController.openFile(file);
            }
        }
    }
    event.setDropCompleted(success);
    event.consume();
}

From source file:Main.java

private void initializeListeners() {
    // drag from left to right
    leftListView.setOnDragDetected(new EventHandler<MouseEvent>() {
        @Override/*from w  w w. ja v a  2s.c om*/
        public void handle(MouseEvent event) {
            if (leftListView.getSelectionModel().getSelectedItem() == null) {
                return;
            }

            Dragboard dragBoard = leftListView.startDragAndDrop(TransferMode.MOVE);
            ClipboardContent content = new ClipboardContent();
            content.putString(leftListView.getSelectionModel().getSelectedItem().getName());
            dragBoard.setContent(content);
        }
    });

    rightListView.setOnDragOver(new EventHandler<DragEvent>() {
        @Override
        public void handle(DragEvent dragEvent) {
            dragEvent.acceptTransferModes(TransferMode.MOVE);
        }
    });

    rightListView.setOnDragDropped(new EventHandler<DragEvent>() {
        @Override
        public void handle(DragEvent dragEvent) {
            String player = dragEvent.getDragboard().getString();
            rightListView.getItems().addAll(new Student(player));
            leftList.remove(new Student(player));
            leftListView.setItems(leftList);
            dragEvent.setDropCompleted(true);
        }
    });
    // drag from right to left
    rightListView.setOnDragDetected(new EventHandler<MouseEvent>() {
        @Override
        public void handle(MouseEvent event) {
            Dragboard dragBoard = rightListView.startDragAndDrop(TransferMode.MOVE);
            ClipboardContent content = new ClipboardContent();
            content.putString(rightListView.getSelectionModel().getSelectedItem().getName());
            dragBoard.setContent(content);
        }
    });

    leftListView.setOnDragOver(new EventHandler<DragEvent>() {
        @Override
        public void handle(DragEvent dragEvent) {
            dragEvent.acceptTransferModes(TransferMode.MOVE);
        }
    });

    leftListView.setOnDragDropped(new EventHandler<DragEvent>() {
        @Override
        public void handle(DragEvent dragEvent) {
            String player = dragEvent.getDragboard().getString();
            leftListView.getItems().remove(new Student(player));

            rightList.remove(new Student(player));
            dragEvent.setDropCompleted(true);
        }
    });
}

From source file:net.rptools.tokentool.controller.ManageOverlays_Controller.java

@FXML
void overlayViewFlowPane_DragDropped(DragEvent event) {
    Dragboard db = event.getDragboard();
    if (db.hasImage()) {
        try {/*from  w  w w  . j  av  a 2s.co  m*/
            // Prompt for name & return file name
            File newOverlayFile = new File(currentDirectory.getCanonicalPath() + "/somefilename.png");
            ImageIO.write(SwingFXUtils.fromFXImage(db.getImage(), null), "png", newOverlayFile);
        } catch (IOException e) {
            log.error("Error writing new overlay image.", e);
        }

        loadImages(overlayTreeView.getSelectionModel().getSelectedItem());
        event.setDropCompleted(true);
    } else if (db.hasFiles()) {
        db.getFiles().forEach(file -> {
            FileSaveUtil.copyFile(file, currentDirectory);
        });
        loadImages(overlayTreeView.getSelectionModel().getSelectedItem());
        event.setDropCompleted(true);
    } else if (db.hasUrl()) {
        FileSaveUtil.copyFile(new File(db.getUrl()), currentDirectory);
        loadImages(overlayTreeView.getSelectionModel().getSelectedItem());
        event.setDropCompleted(true);
    }
}

From source file:dpfmanager.shell.interfaces.gui.component.dessign.DessignView.java

@FXML
protected void onDragEnteredInput(DragEvent event) throws Exception {
    if (acceptedFiles(event.getDragboard(), getController().getAcceptedExetsniosn(), true)) {
        inputText.getStyleClass().add("on-drag");
    }/*from  ww  w .  j a v a  2 s. c o m*/
    event.consume();
}

From source file:dpfmanager.shell.interfaces.gui.component.dessign.DessignView.java

@FXML
protected void onDragOverInput(DragEvent event) throws Exception {
    // Filter accepted files
    if (acceptedFiles(event.getDragboard(), getController().getAcceptedExetsniosn(), true)) {
        event.acceptTransferModes(TransferMode.MOVE);
    }//from  w  w  w.j  a v  a2 s .c  o  m
    event.consume();
}