Home:ALL Converter>Java Fx resizing scene to stage

Java Fx resizing scene to stage

Ask Time:2018-11-14T12:40:54         Author:anupampunith

Json Formatter

I searched but could not find anything close to the problem that I am having with Fx. I am using Java Fx/JDK 8 and having problems with resizing the scene(s). The below code below loads only one screen at a time in scene graph, and switches between the screens. The problem is when I resize the stage. The scene is not being resized with the stage. Nothing fancy in FXML files only anchor pane and a few components.Thanks in advance.

public class ScreensFramework extends Application {

    static String main = "main";
    static String mainFile = "Main.fxml";
    static String play = "play";
    static String playFile = "Play.fxml";


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

    @Override
    public void start(Stage mainStage) {
        ScreensController mainContainer = new ScreensController();
        mainContainer.loadScreen(ScreensFramework.main, ScreensFramework.mainFile);
        mainContainer.loadScreen(ScreensFramework.play, ScreensFramework.playFile);

        mainContainer.setScreen(ScreensFramework.main);

        AnchorPane root = new AnchorPane();
        root.getChildren().setAll(mainContainer);
        Scene scene = new Scene(root);
        mainStage.setScene(scene);
        //This did not work out either
        mainStage.heightProperty().addListener((observable, oldValue, newValue) -> {
        root.getScene().prefHeight((double) newValue);
        });
        mainStage.show();
    }
}

public class MainController implements Initializable, ControlledScreen {

    ScreensController myController;

    @Override
    public void initialize(URL url, ResourceBundle rb) {
    }

    public void setScreenParent(ScreensController screenParent){
        myController = screenParent;
    }
}

public class PlayController implements Initializable, ControlledScreen {

    ScreensController myController;

    @Override
    public void initialize(URL url, ResourceBundle rb) {
    }

    public void setScreenParent(ScreensController screenParent){
        myController = screenParent;
    }
}


public class ScreensController extends AnchorPane {

    private HashMap<String, Node> screens = new HashMap<>();

    public void addScreen(String name, Node screen) {
        screens.put(name, screen);
    }

    public boolean loadScreen(String name, String resource) {
        try {
            FXMLLoader myLoader = new FXMLLoader(getClass().getResource(resource));
            Parent loadScreen = (Parent) myLoader.load();
            ControlledScreen myScreenControler = ((ControlledScreen) myLoader.getController());
            myScreenControler.setScreenParent(this);
            addScreen(name, loadScreen);
            return true;
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return false;
        }
    }

     public boolean setScreen(final String name) {

        if (screens.get(name) != null) {    
            if (!getChildren().isEmpty()) {    
                getChildren().remove(0);                    //remove the displayed screen
                getChildren().add(0, screens.get(name));     //add the screen

            } else {
                getChildren().add(screens.get(name));  
            }
            return true;
        } else {
            return false;
        }               
    }       
}

Author:anupampunith,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/53293294/java-fx-resizing-scene-to-stage
yy