As I understand it, the problem is posted above. I think the scene is good enough to set the preferred height and width, as the listener receives a new window size request. But it has some limitations, if you maximize or minimize the javaFX screen and try to switch to another screen, then the other screen will have the same window size, but the scene content will be distorted by default in height and width, for example, take the login and home scene in javafx (the whole scene is held together with fxml). Login.fxml is initialized by its controller. As you already mentioned, the scene is initialized in the constructor, so it must be the controller of the associated fxml (currently FXML is closely connected with the controller). You are going to set the size of the scene (height and width) in the 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();
2.) The same goes for the 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();
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 putting Stage.hide () before Stage.show () in each controller. Hope this helps you.
source share