That is because the Stage adapts its size to the scene, unless explicitly instructed diferently...
To here is one solution:
stage.setScene(scene2);
stage.setHeight(1000);
stage.setWidth(1000);
And a sample 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() {
@Override
public void handle(ActionEvent arg0) {
// TODO Auto-generated method stub
stage.setScene(scene1);
stage.setHeight(1000);
stage.setWidth(1000);
}
});
boton1.setOnAction(new EventHandler() {
@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);
}
}