Search code examples
javajavafx-2java-7borderpane

BorderPane is hiding bottom region until window is maximized


BorderPane in JavaFx application does not show bottom region Node unless the window is maximized when the scene is switched using Button event. If the scenes are switched one after another its arranged perfectly. Do I have bugs in my code or is this the default behaviour? Thanks. System : Windows XP Java version : 7

My SSCE:

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.geometry.Rectangle2D;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;

public class Main extends Application {
@Override
public void start(final Stage stage) {
    try {
        ///// 2nd scene
        BorderPane root2 = new BorderPane();
        root2.setPrefSize(stage.getWidth(),stage.getHeight());
        HBox buttons2=new HBox(50);
        buttons2.getChildren().addAll(new Button("Button1"),new Button("Button2"));
        buttons2.setAlignment(Pos.BOTTOM_CENTER);
        root2.setBottom(buttons2);
        final Scene scene2 = new Scene(root2,stage.getWidth(),stage.getHeight());
        ///// 1st scene
        VBox buttons1=new VBox();
        buttons1.setPrefSize(stage.getWidth(),stage.getHeight());
        Button nextSceneBtn=new Button("NEXT");
        buttons1.getChildren().add(nextSceneBtn);
        buttons1.setAlignment(Pos.CENTER);
        Scene scene1=new Scene(buttons1,stage.getWidth(),stage.getHeight());

            ////action event
            nextSceneBtn.setOnMouseClicked(new EventHandler<MouseEvent>(){
                @Override
                public void handle(MouseEvent event) {
                    stage.setScene(scene2);
                }

            });
            ///stage
            Screen screen = Screen.getPrimary();
            Rectangle2D bounds = screen.getVisualBounds();
            stage.setX(0);
            stage.setY(0);
            stage.setWidth(bounds.getWidth());
            stage.setHeight(bounds.getHeight());

            stage.setScene(scene1); //if it's #setScene(scene2) at the beginning, it's ok
        stage.show();       
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        launch(args);
    }
}

Solution

  • That looks like a bug, which seems to have been fixed in JavaFX 8. Obviously if you're running on Windows XP, that's of limited use.

    A possible workaround is to switch the root of the scene, instead of the scene itself:

    import javafx.application.Application;
    import javafx.event.EventHandler;
    import javafx.geometry.Pos;
    import javafx.geometry.Rectangle2D;
    import javafx.stage.Screen;
    import javafx.stage.Stage;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.input.MouseEvent;
    import javafx.scene.layout.BorderPane;
    import javafx.scene.layout.HBox;
    import javafx.scene.layout.VBox;
    
    public class Main extends Application {
    @Override
    public void start(final Stage stage) {
        try {
            ///// 2nd scene
            final BorderPane root2 = new BorderPane();
            root2.setPrefSize(stage.getWidth(),stage.getHeight());
            HBox buttons2=new HBox(50);
            buttons2.getChildren().addAll(new Button("Button1"),new Button("Button2"));
            buttons2.setAlignment(Pos.BOTTOM_CENTER);
            root2.setBottom(buttons2);
    //        final Scene scene2 = new Scene(root2,stage.getWidth(),stage.getHeight());
            ///// 1st scene
            VBox buttons1=new VBox();
            buttons1.setPrefSize(stage.getWidth(),stage.getHeight());
            Button nextSceneBtn=new Button("NEXT");
            buttons1.getChildren().add(nextSceneBtn);
            buttons1.setAlignment(Pos.CENTER);
            final Scene scene1=new Scene(buttons1,stage.getWidth(),stage.getHeight());
    
                ////action event
                nextSceneBtn.setOnMouseClicked(new EventHandler<MouseEvent>(){
                    @Override
                    public void handle(MouseEvent event) {
    //                    stage.setScene(scene2);
                        scene1.setRoot(root2);
                    }
    
                });
                ///stage
                Screen screen = Screen.getPrimary();
                Rectangle2D bounds = screen.getVisualBounds();
                stage.setX(0);
                stage.setY(0);
                stage.setWidth(bounds.getWidth());
                stage.setHeight(bounds.getHeight());
    
                stage.setScene(scene1); //if it's #setScene(scene2) at the beginning, it's ok
            stage.show();       
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
        public static void main(String[] args) {
            launch(args);
        }
    }