Set height and width of scene and scene in javafx

  • I am developing one javafx application.
  • My application has two scenes and one stage.
  • In the application, the height and width for both scenes are the same or constant.
  • since in my study the height and width of the scene remain constant, which are mentioned in the constructor, but the scene is adjusted with the height and width of the scene.
  • when I dine with the height and width of the scene, which is different from the constant height and width of the scene, then the scene is adjusted with the scene.
  • but when at runtime, when I apply the 2nd scene, the scene is not adjusted with the height and width of the scene. The height and width of the scene remain constant.

  • so any solution?

+6
source share
3 answers

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(); // 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<Number>() { @Override public void changed(ObservableValue<? extends Number> observableValue, Number number, Number number2) { setCurrentWidthToStage(number2); } }); stage.heightProperty().addListener(new ChangeListener<Number>() { @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.) 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(); // 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 putting Stage.hide () before Stage.show () in each controller. Hope this helps you.

+4
source

This is because Stage adapts its size to the scene, unless explicitly stated otherwise ...

Here is one solution:

 stage.setScene(scene2); stage.setHeight(1000); stage.setWidth(1000); 

And an example application:

 import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.AnchorPane; import javafx.stage.Stage; public class Test extends Application { @Override public void start(final Stage stage) throws Exception { AnchorPane anchor1 = new AnchorPane(); final Scene scene1 = new Scene(anchor1, 250, 250); Button boton1 = new Button(); anchor1.getChildren().add(boton1); AnchorPane anchor2 = new AnchorPane(); final Scene scene2 = new Scene(anchor2, 500, 500); Button boton2 = new Button(); anchor2.getChildren().add(boton2); boton2.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent arg0) { // TODO Auto-generated method stub stage.setScene(scene1); stage.setHeight(1000); stage.setWidth(1000); } }); boton1.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent arg0) { // TODO Auto-generated method stub stage.setScene(scene2); stage.setHeight(1000); stage.setWidth(1000); } }); stage.setScene(scene1); stage.show(); } public static void main(String[] args) { launch(args); } } 
+6
source

I know this is an old topic, but this (bug?) Is still persisted in Java7u75.

I ran into the same problem ... the solution proposed by shambhu above does work, but leads to the visible “window switch” effect:

 stage.hide(); stage.show(); 

I found that the following code solves the problem (gets the stage to “upgrade”) without any visible effects:

 final boolean resizable = stage.isResizable(); stage.setResizable(!resizable); stage.setResizable(resizable); 
+1
source

Source: https://habr.com/ru/post/954560/


All Articles