Search code examples
browsertextjavafxdrag-and-dropstage

Drag text from browser into JavaFX GUI


I can't find any question exactly like this: Is there a way in JavaFX to display a GUI (stage) that accepts text that a user drap-and-drops from a browser?

For instance, the user navigates to a certain URL, then copies all of the page's text and drags it into the JavaFX stage displayed. The text can then be used within the Java program. I'd prefer not to use Selenium so that my app doesn't perform any scrape-like activities.

I'm looking for a solution compatible with Windows XP+ and all browsers.

Any feedback regarding starting points, tutorials, posts or limitations is great. Thank you


Solution

  • You may try something like this:

    public class MainApp extends Application {
    
        @Override
        public void start(Stage stage) throws Exception {
            TextField textField = new TextField();
            textField.setPromptText("Drag text here");
            textField.addEventHandler(
                    DragEvent.DRAG_OVER,
                    event -> {
                        if (event.getDragboard().hasString()) {
                            event.acceptTransferModes(TransferMode.COPY);
                        }
                        event.consume();
                    });
            textField.addEventHandler(
                    DragEvent.DRAG_DROPPED,
                    event -> {
                        Dragboard dragboard = event.getDragboard();
                        if (event.getTransferMode() == TransferMode.COPY && 
                                dragboard.hasString()) {
                            textField.setText(dragboard.getString());
                            event.setDropCompleted(true);
                        }
                        event.consume();
                    });
            StackPane stackPane = new StackPane(textField);
            stackPane.setPadding(new Insets(5));
            stage.setScene(new Scene(stackPane, 300, 150));
            stage.setTitle("Drag and Drop");
            stage.show();
        }
    
        public static void main(String[] args) {
            MainApp.launch(args);
        }
    
    }
    

    Getting HTML content

        TextArea textArea = new TextArea();
            textArea.setPromptText("Drag text here");
            textArea.addEventHandler(
                    DragEvent.DRAG_OVER,
                    event -> {
                        if (event.getDragboard().hasHtml()) {
                            event.acceptTransferModes(TransferMode.COPY);
                        }
                        event.consume();
                    });
            textArea.addEventHandler(
                    DragEvent.DRAG_DROPPED,
                    event -> {
                        Dragboard dragboard = event.getDragboard();
                        if (event.getTransferMode() == TransferMode.COPY && 
                                dragboard.hasHtml()) {
                            textArea.setText(dragboard.getHtml());
                            event.setDropCompleted(true);
                        }
                        event.consume();
                    });