2

Should a JavaFX partially obscured window still receives mouse moved events in the a portion of the window that is obscured by another window? This seems like odd behaviour. Adding extra windows (javaFX in the same application or other applications windows) results in odd and unpredictable behaviour

Am I doing anything wrong?

Is there away of avoiding this behviour?

package obscuredmouseevents;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

/**
 *
 * @author michaelellis
 */
public class ObscuredMouseEvents extends Application {

    /**
     * After launching drag the mouse from left to right through the left edge
     * of the light blue panel of Window 1 and see the mouse coordinates
     * displayed in the text area at the bottom of Window 1. However, continue
     * dragging into the light blue panel of Window 2 and see not only the mouse
     * coordinates displayed in the text area at the bottom of Window 2, but
     * also the mouse coordinates displayed in the text area at the bottom of
     * Window 1.
     * <p>
     * Clicking in Window 2 stops mouse events being registered in Window 1,
     * however moving the mouse back into the light blue panel of Window 1, and
     * on into the light blue panel of Window 2 see the problem occur again.
     *
     * @param primaryStage
     */

    @Override
    public void start(Stage primaryStage) {
        createNewWindow("Window 1", 100, 100).show();
        createNewWindow("Window 2", 160, 120).show();
    }

    Stage createNewWindow(String title, int x, int y) {

        BorderPane root = new BorderPane();
        Rectangle r = new Rectangle(300, 200);
        r.setFill(Color.ALICEBLUE);
        root.setCenter(r);

        final TextField tf = new TextField();
        root.setBottom(tf);

        r.setOnMouseMoved(e -> {
            tf.setText(String.format("%d,%d", (int) e.getX(), (int) e.getY()));
            e.consume();
        });

        Scene scene = new Scene(root, 300, 300);
        Stage stage = new Stage();
        stage.setX(x);
        stage.setY(y);

        stage.setTitle(title);
        stage.setScene(scene);
        return stage;
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}

OS-X El Capitan 10.11.6 (15G1421)

MacBook Pro (Retina, 15-inch, Early 2013)

java version "1.8.0_121" Java(TM) SE Runtime Environment (build 1.8.0_121-b13)

Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)

Added on 22-DEC-2017 for further exploration into the problem The following example adds mouse enter/exit handling and a third window. There is some very strange and erratic behaviour. Try also clicking in a front window which stops mouse moved events being delivered into obscured windows, and also reordering stack position of windows which shows more erratic behaviour regarding the delivery of events to obscured windows!

package obscuredmouseevents;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class ObscuredMouseEvents extends Application {

    @Override
    public void start(Stage primaryStage) {
        createNewWindow("Window 1", 100, 100).show();
        createNewWindow("Window 2", 150, 120).show();
        createNewWindow("Window 3", 200, 140).show();
    }

    Stage createNewWindow(String title, int x, int y) {

        BorderPane root = new BorderPane();
        Rectangle r = new Rectangle(280, 240);
        r.setFill(Color.ALICEBLUE);
        root.setCenter(r);

        final CheckBox checkBox = new CheckBox("inside");
        root.setTop(checkBox);

        final TextField tf = new TextField();
        root.setBottom(tf);

        r.setOnMouseMoved(e -> {
            tf.setText(String.format("%d,%d", (int) e.getX(), (int) e.getY()));
            e.consume();
        });
        r.setOnMouseEntered(e -> {
            checkBox.setSelected(true);
            e.consume();
        });
        r.setOnMouseExited(e -> {
            checkBox.setSelected(false);
            e.consume();
        });

        Scene scene = new Scene(root, 300, 300);
        Stage stage = new Stage();
        stage.setX(x);
        stage.setY(y);

        stage.setTitle(title);
        stage.setScene(scene);
        return stage;
    }

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

}
Michael Ellis
  • 243
  • 3
  • 14
  • Cannot reproduce the issue... Perhaps add some details like java version, OS, ect. – fabian Dec 21 '17 at 18:12
  • Did you tried to implement On Mouse Entered, On Mouse Exited? – zlakad Dec 21 '17 at 18:15
  • Reproduces for me on Mac OS X 10.12.6, JDK 1.8.0. I think this is probably OS dependent; the windows are "native" and the determination of which mouse events are passed to which windows is almost certainly made at the OS level. In general, Mac and Windows behave quite differently this way: e.g. mouse/trackpad based scrolling on a Mac is generally directed to the window over which the scroll occurs, irrespective of which window is active; the same is not true on Windows. – James_D Dec 21 '17 at 18:31
  • OS-X El Capitan 10.11.6 (15G1421) MacBook Pro (Retina, 15-inch, Early 2013) java version "1.8.0_121" Java(TM) SE Runtime Environment (build 1.8.0_121-b13) Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode) Thanks James_D makes sense about the platform specific nature. Also @zlakad, yes I have a more sophisticated version that catches the mouse entered/exited events and weird stuff happens there too. There is, I believe some bugs in the OS-X mouse/window event handling code. – Michael Ellis Dec 22 '17 at 19:16

0 Answers0