I´ve got 2 different screens, in the first screen the user can load images and the other one is just a single button (the stage is invisible so the stage has to be different
if the primaryStage
is where "user can load images" do :
static Stage primaryStage;
@FXML
private void goToScreen1(ActionEvent event) throws Exception{
Stage stage = (Stage) showStage.getScene().getWindow();
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/sample.fxml"));
Parent root = fxmlLoader.load();
if(primaryStage==null)
primaryStage = new Stage();
primaryStage.setResizable(true);
primaryStage.setOpacity(0.0);
primaryStage.show();
primaryStage.setX(0);
primaryStage.setY(0);
primaryStage.setHeight(primScreenBounds.getHeight());
primaryStage.setWidth(primScreenBounds.getWidth() / 2);
}
You have to store a reference to your stage content, so you don't have to reinstantiate it. One possibility is to store it in the mainController and provide an EventHandler to your subController, to switch the stage
public interface ScreenController {
void setOnSwitchStage(EventHandler<ActionEvent> handler);
}
public class Screen1Controller implements Initializable, ScreenController {
@FXML
private Button btnSwitchScreen
public Screen1Controller() {
}
@Override
public void initialize(URL location, ResourceBundle resources) {
}
@Override
public void setOnSwitchStage(EventHandler<ActionEvent> handler) {
btnSwitchScreen.setOnAction(handler);
}
}
public class YourApp extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Stage secondaryStage = new Stage();
initScreen(primaryStage, secondaryStage, "screen1.fxml");
initScreen(secondaryStage, primaryStage, "screen2.fxml");
primaryStage.show();
}
private void initScreen(Stage parentStage, Stage nextStage, String resource) throws IOException {
FXMLLoader loader = new FXMLLoader(getClass().getResource(resource));
Scene scene = new Scene(loader.load());
parentStage.setScene(scene);
ScreenController screenController = loader.getController();
screenController.setOnSwitchStage(evt -> {
nextStage.show();
parentStage.hide();
});
}
public static void main(String[] args) {
launch(args);
}
}