1

I've tried numerous things to make this scene fit the stage (as other scenes in my application do) but I can't figure it out.

I'm currently using buttons and controllers to switch scenes, with the aim of maintaining an MVC format, so I'm using the line stage.setScene(new Scene(root pane), desired width, desired height).

However the flicker that comes with switching scenes ruins the seamlessness of my application.

I have tried other suggestions like stage.centerOnScreen() but this seems to shift the whole stage out of view (cuts half the stage off), I have also tried stage.sizeToScreen(), however this seemed to make no difference.

Ideally, I'd like to use stage.getScene().setRoot(pane), but the scene seems to change size or creates a white border instead of resizing the stage to fit the scene (as shown).

If I hadn't made myself clear thus far, I am trying to get the scene to fit the stage. The scene's root pane is the desired size when it is set as the root, but for some reason the scene changes size.

This is how I'd like the scene to fit the stage (this is scene 1)

enter image description here

but this is what happens when I change the root of the scene to the next page.

enter image description here

So, ideally I'm looking for a way to use setRoot(pane), getting the scene to fit the stage or use setScene(scene) without the flickering.

Code examples:

In one of my controllers, I have this (working as intended):

public void handle(MouseEvent event) {
    if (event.getSource() instanceof Button) {
        Button sourceButton = (Button)event.getSource();
        Stage primaryStage = (Stage ((Node)event.getSource()).getScene().getWindow();

        if (sourceButton.getId().equals("back-button")) {
            primaryStage.getScene().setRoot(view.getWelcomePane());
        }
    }
}

In the view:

public Pane getWelcomePane() {
    return new BorderPane(bpWelcomeMain); // bpWelcomeMain is the border pane that contains all the page components
}

In the other controller, when it appears how I want I have to use this (but this produces flickering):

public void handle(MouseEvent event) {

    if (event.getSource() instanceof Button) {

        Button clickedButton = (Button)event.getSource();
        Stage primaryStage = (Stage)((Node)event.getSource()).getScene().getWindow();

        if (clickedButton.getId().equals("create-button")) {
            primaryStage.setScene(view.getCreateScene());
        }
}

In the view:

public Scene getCreateScene() {
    return new Scene(bpCreateMain, 1500, 750); // bpCreateMain is the border pane that contains all the page components
}

However I would like to use .setRoot() instead of setScene() as shown in the first example to remove the flickering but also need to have it appear as intended (so I would like to use my getCreatePane() method in this view - similar to getWelcomePane() instead of getCreateScne()), the methods are practically the same so I am unsure why it is not working.

JIrv
  • 71
  • 2
  • 10
  • Possible duplicate of [JavaFX equivalent of Swing's pack()](https://stackoverflow.com/questions/14099797/javafx-equivalent-of-swings-pack) – Vince Nov 16 '18 at 23:27
  • I've also tried sizeToScreen(), nothing changes. – JIrv Nov 16 '18 at 23:32
  • 3
    @Jlrv Please provide a [MVCE](https://stackoverflow.com/help/mcve) that reproduces the issue – Vince Nov 16 '18 at 23:44
  • I think that code (mcve ) would have been clearer and shorter than the attempt to describe it. – c0der Nov 17 '18 at 04:33

0 Answers0