I'm experiencing a problem with the layout after changing scenes is JavaFX when monitor scaling is set to 125%.
When scaling is set to 100% everything works fine.
I'm using JavaFX 11.0.2.
Here is my code:
import javafx.application.Application;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
public class App extends Application {
private Scene firstScene;
private Scene secondScene;
private Rectangle2D screenBounds = Screen.getPrimary().getBounds();
private final int backButtonSize = (int) (screenBounds.getWidth() / 50);
private Image image = new Image("https://imgur.com/download/uDlKu6Y");
@Override
public void start(Stage stage) {
Button forwardButton = new Button("Forward");
forwardButton.setOnAction(actionEvent -> stage.setScene(secondScene));
firstScene = new Scene(new StackPane(forwardButton));
Button backButton = new Button("Back");
backButton.setOnAction(actionEvent -> stage.setScene(firstScene));
backButton.setMinWidth(backButtonSize);
ImageView imageView = new ImageView(image);
imageView.setFitWidth(screenBounds.getWidth() - backButtonSize);
imageView.setFitHeight(screenBounds.getHeight());
imageView.setPreserveRatio(true);
secondScene = new Scene(new BorderPane(imageView, null, null, null, backButton));
stage.setX(screenBounds.getMinX());
stage.setY(screenBounds.getMinY());
stage.setWidth(screenBounds.getWidth());
stage.setHeight(screenBounds.getHeight());
stage.initStyle(StageStyle.UNDECORATED);
stage.setScene(firstScene);
stage.show();
}
}
The first scene that I set in stage (stage.setScene(firstScene)
or stage.setScene(secondScene)
) looks fine.
But after clicking on a button and changing scenes (calling event handlers from forwardButton.setOnAction(...)
or backButton.setOnAction(...)
), the layout is broken.
I would appreciate any help.
P.S. If anyone could suggest a way to make ImageView
take the whole vertical space, I would also appreciate this.
But it is considered a much smaller problem right now.
Edit 1:
I've managed to fix the problem by changing panes, not scenes.
But I'm still wondering why the first attempt was unsuccessful.
Here is the updated code:
import javafx.application.Application;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
public class App extends Application {
private Scene scene;
private Pane firstPane;
private Pane secondPane;
private Rectangle2D screenBounds = Screen.getPrimary().getBounds();
private final int backButtonSize = (int) (screenBounds.getWidth() / 50);
private Image image = new Image("https://imgur.com/download/uDlKu6Y");
@Override
public void start(Stage stage) {
Button forwardButton = new Button("Forward");
forwardButton.setOnAction(actionEvent -> scene.setRoot(secondPane));
firstPane = new StackPane(forwardButton);
Button backButton = new Button("Back");
backButton.setOnAction(actionEvent -> scene.setRoot(firstPane));
backButton.setMinWidth(backButtonSize);
ImageView imageView = new ImageView(image);
imageView.setFitWidth(screenBounds.getWidth() - backButtonSize);
imageView.setFitHeight(screenBounds.getHeight());
imageView.setPreserveRatio(true);
secondPane = new BorderPane(imageView, null, null, null, backButton);
scene = new Scene(firstPane);
stage.setX(screenBounds.getMinX());
stage.setY(screenBounds.getMinY());
stage.setWidth(screenBounds.getWidth());
stage.setHeight(screenBounds.getHeight());
stage.initStyle(StageStyle.UNDECORATED);
stage.setScene(scene);
stage.show();
}
}