0

I have a client application, and I want to get the server address, port, and some other info from the user in order to initialize the controller of the main stage. Currently my code look like this

public class MemoryGameClient extends Application {
    @Override
    public void start(Stage mainStage) throws Exception {
        FXMLLoader fxml = new FXMLLoader(getClass().getResource("MemoryGameClient.fxml"));
        MemoryGameClientController controller = fxml.getController()
        Parent root = loader.load();
        controller.connect(SERVER_ADDRESS, PORT, GAME_BOARD_SIZE);
        Scene scene = new Scene(root);
        mainStage.setScene(scene);
        mainStage.show()
    }

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

It works fine using the hardcoded values, but I want to be able to open a DialogPane or something like that to get those values from user before initializing the scene and running the main application logic.

Can I set an empty Scene that launch a dialog and after that quitting and starting the main stage? Can I do that from the controller before mainStage.show()?

(I need the user input not only for connecting the server but also to determine the size of the GridPane in root)

jewelsea
  • 150,031
  • 14
  • 366
  • 406
CforLinux
  • 267
  • 2
  • 14
  • 6
    Technically, you shouldn't be connecting to an external service on the FXAT from the controller. As soon as you start putting in the logic to handle the connection on a background thread you'll realize that this linear approach of "connect then display" is not such a good idea. Anyways, launch your screen with it disconnected, then create Dialog to get the connection details and call its showAndWait(). Then connect using Task on a Background thread and handle the screen updates for connected in its onSucceeded() method. – DaveB Jan 26 '23 at 21:33
  • This [example](https://stackoverflow.com/a/73528614/230513) illustrates the typical life cycle. – trashgod Jan 26 '23 at 23:24
  • 1
    Take a look at this [Task based splash screen startup](https://gist.github.com/jewelsea/2305098). You could modify it to collect user input rather than monitor progess, and have the Task perform your connection as suggested by DaveB. [A similar example](https://gist.github.com/jewelsea/1588531). – jewelsea Jan 27 '23 at 01:20

0 Answers0