Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8349972

JavaFX Window X,Y Set Not Working In Multi Monitor Setup

XMLWordPrintable

      ADDITIONAL SYSTEM INFORMATION :
      RHEL 8.10 using Gnome 3 paired with openJDK/JFX 17.0.14

      A DESCRIPTION OF THE PROBLEM :
      primaryStage.setX(x) and primaryStage.setY(y) do not result in the window being placed at the specified location when the result of applying the XY would result in the window spanning 2 monitors. When this case is encountered, the window is forced to display on one monitor of the other, neither of which match the coordinates that are applied.

      REGRESSION : Last worked in version 8

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Assuming you have two monitors that are setup up right next to each other with a resolution of 1920x1200. Update via code a window (window dimensions 500x300) X,Y coordinate to be be at X=1900 and y=300.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The origin of the window is displayed at (1900,300), which displays the window across the windows.
      ACTUAL -
      Window is forced completely to one monitor.

      ---------- BEGIN SOURCE ----------
      import javafx.application.Application;
      import javafx.geometry.Insets;
      import javafx.geometry.Rectangle2D;
      import javafx.scene.Scene;
      import javafx.scene.control.Button;
      import javafx.scene.control.Label;
      import javafx.scene.control.TextField;
      import javafx.scene.layout.GridPane;
      import javafx.stage.Screen;
      import javafx.stage.Stage;
      import javafx.stage.StageStyle;

      public class WindowMoveApp extends Application {

          private Label xLabel;
          private Label yLabel;

          @Override
          public void start(Stage primaryStage) {

      // Create the grid layout
      GridPane grid = new GridPane();
              grid.setPadding(new Insets(10, 10, 10, 10));
              grid.setVgap(8);
              grid.setHgap(10);

              // Create the label and text field for "x"
              Label xTextLabel = new Label("x:");
              GridPane.setConstraints(xTextLabel, 0, 0);
              TextField xInput = new TextField();
              GridPane.setConstraints(xInput, 1, 0);

              // Create the label and text field for "y"
              Label yTextLabel = new Label("y:");
              GridPane.setConstraints(yTextLabel, 0, 1);
              TextField yInput = new TextField();
              GridPane.setConstraints(yInput, 1, 1);

              // Create the "Move" button
              Button moveButton = new Button("Move");
              GridPane.setConstraints(moveButton, 1, 2);

              // Create labels to display the window's current position
              xLabel = new Label("x: ");
              GridPane.setConstraints(xLabel, 0, 3);
              yLabel = new Label("y: ");
              GridPane.setConstraints(yLabel, 1, 3);

              // Add all components to the grid
              grid.getChildren().addAll(xTextLabel, xInput, yTextLabel, yInput, moveButton, xLabel, yLabel);

              // Move button action
              moveButton.setOnAction(event -> {
                  try {
                      // Parse x and y values from the input fields

                      int xPos = Integer.parseInt(xInput.getText());
                      int yPos = Integer.parseInt(yInput.getText());

      // Move the window to the specified coordinates
                      primaryStage.setX(xPos);
                      primaryStage.setY(yPos);

      //undo transparent
                  } catch (NumberFormatException e) {
                      // Handle invalid input
                      System.out.println("Please enter valid integer values for x and y.");
                  }
              });

              // Update position labels whenever the window is moved
              primaryStage.setOnShown(event -> updatePositionLabels(primaryStage));
              primaryStage.setOnCloseRequest(event -> updatePositionLabels(primaryStage));
              primaryStage.setOnHiding(event -> updatePositionLabels(primaryStage));

              // Set up the scene and stage
              Scene scene = new Scene(grid, 300, 200);
              primaryStage.setTitle("Window Move");
              primaryStage.setScene(scene);
              primaryStage.show();

              // Listener to update the labels whenever the window is moved
              primaryStage.xProperty().addListener((obs, oldX, newX) -> updatePositionLabels(primaryStage));
              primaryStage.yProperty().addListener((obs, oldY, newY) -> updatePositionLabels(primaryStage));

              primaryStage.show();
          }

          // Method to update x and y labels with current window position
          private void updatePositionLabels(Stage stage) {
              double xPos = stage.getX();
              double yPos = stage.getY();
      Screen screenNum = Screen.getScreensForRectangle(xPos, yPos, 300, 200).get(0);
              xLabel.setText("x: " + (int) xPos);
              yLabel.setText("y: " + (int) yPos);
          }

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

            kcr Kevin Rushforth
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: