Issue With Drag-n-Drop

Sirish Vadala sirishreddy at gmail.com
Mon Aug 10 18:27:36 UTC 2015


«« NOTE: I have posted the same question on stackoverflow.com
<http://stackoverflow.com/questions/31886709/drag-and-drop-an-email-attachment-into-a-javafx-8-application>
. »»

I am trying to implement a requirement to drag and drop documents attached
in an email into a JavaFX 8 application running on jdk 8b45. I am able to
drag and drop from any folder on my computer but not from an email
attachment [Running on Microsoft Outlook Professional Plus 2010].

// MY VBOX WHERE I WANT TO DROP FILES INTO
VBox bkgrndDocsVBox = new VBox(10.0);
bkgrndDocsVBox.setPadding(new Insets(15, 10, 5, 10));

bkgrndDocsVBox.setStyle("-fx-border-color: transparent;");
bkgrndDocsVBox.setOnDragOver((final DragEvent event) -> {
    mouseDragOver(event, bkgrndDocsVBox);
});
bkgrndDocsVBox.setOnDragDropped((final DragEvent event) -> {
    mouseDragDropped(event, backgroundDocsDataTable);
});
bkgrndDocsVBox.setOnDragExited((final DragEvent event) -> {
    bkgrndDocsVBox.setStyle("-fx-border-color: transparent;");
});

..............................
..............................

private void mouseDragOver(DragEvent dragEvent, VBox bkgrndDocsVBox) {
    final Dragboard dragboard = dragEvent.getDragboard();

    System.out.println("dragboard.hasFiles()::"+dragboard.hasFiles());

    if (dragboard.hasFiles()) {
        bkgrndDocsVBox.setStyle("-fx-border-color: green;");
        dragEvent.acceptTransferModes(TransferMode.ANY);
    } else {
        dragEvent.consume();
    }
}

..............................
..............................
private void mouseDragDropped(DragEvent dragEvent, TableView<BgDocBean>
bgDocsTable) {

    System.out.println("ENTER mouseDragDropped");
    final Dragboard dragBoard = dragEvent.getDragboard();
    boolean success = false;
    boolean isAccepted = false;

// SAVE the FILES into the DATABASE
.......... .......... .......... ..........
.......... .......... .......... ..........
}
The above code works when I try to drag and drop files from a windows
folder. However when I try to drag and drop files from an email attachment,
the 'dragboard.hasFiles()::false' is displayed on the console and the
functionality does not work.

Please see the fully functional POC below:

import javafx.util.Duration;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.input.*;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class HelloDragAndDrop extends Application {

    @Override
    public void start(Stage stage) {
        stage.setTitle("Hello Drag And Drop");

        VBox root = new VBox();
        root.setStyle("-fx-border-color: transparent;");
        root.setAlignment(Pos.CENTER);
        Scene scene = new Scene(root, 400, 200);
        scene.setFill(Color.WHITESMOKE);

        Text target = new Text("DROP HERE");
        target.setScaleX(2.0);
        target.setScaleY(2.0);

        root.setOnDragOver((DragEvent event) -> {
            System.out.println("onDragOver");
            System.out.println("event.getDragboard().hasFiles()::" +
 event.getDragboard().hasFiles());
            if (event.getGestureSource() != root &&
event.getDragboard().hasFiles()) {
                event.acceptTransferModes(TransferMode.ANY);
            }
            event.consume();
        });

        root.setOnDragEntered((DragEvent event) -> {
            System.out.println("onDragEntered");
            if (event.getGestureSource() != root &&
event.getDragboard().hasFiles()) {
                root.setStyle("-fx-border-color: green;");
            }
            event.consume();
        });

        root.setOnDragExited((DragEvent event) -> {
            root.setStyle("-fx-border-color: transparent;");
            event.consume();
        });

        root.setOnDragDropped((DragEvent event) -> {
            System.out.println("onDragDropped");
            Dragboard db = event.getDragboard();
            System.out.println("db.hasFiles()::" + db.hasFiles());
            boolean success = false;
            if (db.hasFiles()) {
                target.setText("SUCCESSFULLY DROPPED");
                success = true;
            }
            event.setDropCompleted(success);
            event.consume();

            Timeline timeline = new Timeline(
                new KeyFrame(Duration.seconds(2), (ActionEvent actionEvent)
-> {
                        target.setText("DROP HERE");
            }),
                    new KeyFrame(Duration.seconds(5))
            );
            timeline.setCycleCount(1);
            timeline.play();

        });

        root.getChildren().add(target);
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        Application.launch(args);
    }
}

On my console it always displays 'event.getDragboard().hasFiles()::false'
when I drag and drop an attachment from Microsoft Outlook Professional Plus
2010.

I would highly appreciate any hints on how this could be successfully
implemented. Thanks.

Thanks. -SV-


More information about the openjfx-dev mailing list