javajavafxdragjxbrowser

setOnMouseDragged not working on browser view


I'm writing a simple application in java using JxBrowser engine but i'm stuck at the very beginning. In my code, there is an undecorated stage that i want to make it draggable. To do so, searched and found the following link:

How to drag undecorated window

So I set mousePressed and MouseDragged event on stackPane but only mousePressed event gets fired and mouseDragged event no way gets fired. Any idea of what's the problem?

Thanks in advance.

import com.teamdev.jxbrowser.chromium.Browser;
import com.teamdev.jxbrowser.chromium.javafx.BrowserView;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

private static double xOffset = 0;
private static double yOffset = 0;

public class Main extends Application {

    public static void main(String[] args) {

        launch(args);

    }

    @Override
    public void start(Stage primaryStage) {

        Platform.setImplicitExit(false);

        Browser browser = new Browser();

        BrowserView browserView = new BrowserView(browser);

        StackPane pane = new StackPane();

        pane.getChildren().add(browserView);

        Scene scene = new Scene(pane, 380, 500);

        primaryStage.initStyle(StageStyle.UNDECORATED);

        primaryStage.setScene(scene);

        pane.setOnMousePressed(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {

                System.out.println("mouse pressed");

                xOffset = primaryStage.getX() - event.getScreenX();

                yOffset = primaryStage.getY() - event.getScreenY();

            }

        });

        pane.setOnMouseDragged(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {

                System.out.println("mouse dragged");

                primaryStage.setX(event.getScreenX() + xOffset);

                primaryStage.setY(event.getScreenY() + yOffset);

            }

        });

        primaryStage.show();

    }

}

Solution

  • Since jxbrowser requires license I couldn't test it... So I replaced that with Label and it works fine.. So my guess is that you are trying to drag by clicking on the browser itself and not the StackPane.. Try clicking at the corner of the Stage or else add stack pane to VBox and setPadding to it.. And try clicking at the corner.. If your clicking on the browser then browser's mouse events will be trigerred..

    Proof: enter image description here

    The correct code

    package RezRem;
    
    import com.teamdev.jxbrowser.chromium.Browser;
    import com.teamdev.jxbrowser.chromium.javafx.BrowserView;
    
    import javafx.application.Application;
    import javafx.application.Platform;
    import javafx.event.EventHandler;
    import javafx.geometry.Insets;
    import javafx.scene.Scene;
    import javafx.scene.input.MouseEvent;
    import javafx.scene.layout.StackPane;
    import javafx.scene.layout.VBox;
    import javafx.stage.Stage;
    import javafx.stage.StageStyle;
    
    
    public class Main extends Application {
    
    private static double xOffset = 0;
    private static double yOffset = 0;
    public static void main(String[] args) {
    
        launch(args);
    
    }
    
    @Override
    public void start(Stage primaryStage) {
    
        Platform.setImplicitExit(false);
    
    
        Browser browser = new Browser();
    
        BrowserView browserView = new BrowserView(browser);
        StackPane pane = new StackPane();
    
        pane.getChildren().add(browserView);
        pane.setPadding(new Insets(10,10,10,10));
        Scene scene = new Scene(pane, 380, 500);
    
        primaryStage.initStyle(StageStyle.UNDECORATED);
    
        primaryStage.setScene(scene);
    
        pane.setOnMousePressed(new EventHandler<MouseEvent>() {
    
            @Override
            public void handle(MouseEvent event) {
    
                System.out.println("mouse pressed");
    
                xOffset = primaryStage.getX() - event.getScreenX();
    
                yOffset = primaryStage.getY() - event.getScreenY();
    
            }
    
        });
    
        pane.setOnMouseDragged(new EventHandler<MouseEvent>() {
    
            @Override
            public void handle(MouseEvent event) {
    
                System.out.println("mouse dragged");
    
                primaryStage.setX(event.getScreenX() + xOffset);
    
                primaryStage.setY(event.getScreenY() + yOffset);
    
            }
    
        });
        browser.loadURL("http://www.google.com");
        primaryStage.show();
    
    }
    
    }
    

    Reason

    The jxbrowser extends till the edge so the stack pane wasn't on top neither was it visible on the sides so the mouse listener's never got triggered by setting the padding of stack pane there was 10px gap on all four sides where, if clicked, triggered the mouse events and thereby solves the problem..