As I understand the problem above posted. I think the stage is good enough to set the preferred height and width as per the listener get the newer request to apply on the windows size. But it has some limitations, if you maximize or minimize the javaFX screen and will try to navigate to other screen then other screen will be having same window size but the scene content will distorted into the default height and width of it, e.g take a login and home scene in javafx (all scene is screated with fxml). Login.fxml is initialized by its controller. As you have mentioned that scene is initialized in constructor, so it must be the controller of the related fxml(as of now FXML is tight coupled with controller). You are going to set the scene size(height & width) in constructor itself.
1.) LoginController for login.fxml
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
class LoginController {
private Stage stage;
private Scene scene;
private Parent parent;
@FXML
private Button gotoHomeButton;
public LoginController() throws Exception {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/fxml/login.fxml"));
fxmlLoader.setController(this);
try {
parent = (Parent) fxmlLoader.load();
// set height and width here for this login scene
scene = new Scene(parent, 1000, 800);
} catch (IOException ex) {
System.out.println("Error displaying login window");
throw new RuntimeException(ex);
}
}
// create a launcher method for this. Here I am going to take like below--
public void launchLoginScene(Stage stage) {
this.stage = stage;
stage.setScene(scene);
stage.setResizable(true);
stage.widthProperty().addListener(new ChangeListener() {
@Override
public void changed(ObservableValue extends Number> observableValue, Number number, Number number2) {
setCurrentWidthToStage(number2);
}
});
stage.heightProperty().addListener(new ChangeListener() {
@Override
public void changed(ObservableValue extends Number> observableValue, Number number, Number number2) {
setCurrentHeightToStage(number2);
}
});
//Don't forget to add below code in every controller
stage.hide();
stage.show();
}
@FXML
public void authenticateUser(ActionEvent actionEvent) {
// write your logic to authenticate user
//
new HomeController().displayHomeScreen(stage);
}
private void setCurrentWidthToStage(Number number2) {
stage.setWidth((double) number2);
}
private void setCurrentHeightToStage(Number number2) {
stage.setHeight((double) number2);
}
}
2.) Same for HomeController --
public class HomeController {
private Parent parent;
private Stage stage;
private Scene scene;
public HomeController (){
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/fxml/home.fxml"));
fxmlLoader.setController(this);
try {
parent = (Parent) fxmlLoader.load();
// set height and width here for this home scene
scene = new Scene(parent, 1000, 800);
} catch (IOException e) {
// manage the exception
}
}
public void displayHomeScreen(Stage stage){
this.stage = stage;
stage.setScene(scene);
// Must write
stage.hide()
stage.show();
}
}
3.) Main class
import javafx.application.Application;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
new LoginController().launchLoginScene(primaryStage);
}
public static void main(String[] args) {
launch(args);
}
}
Just try to put Stage.hide() before Stage.show() in every controller. I hope this will help you out.