alwaysboots
alwaysboots

Reputation: 141

JavaFX - How to create many windows from your main method?

I want to be able create multiple charts each in different windows, but I am struggling to do so in JavaFX. Because the UI is an 'extra' of the application, I wanted to launch the UI separately and separate it from the main program logic, thus I have the following code:

public class UI extends Application {
    private Stage stage;

    @Override
    public void start(Stage stage) {
        this.stage = stage; // I don't really plan on even using this, but I've left this here so that I can break on it.
    }

    public Stage getStage() {
        return this.stage;
    }
}

public class Program {
    public static void main(String[] args) {
        // do other stuff

        Application.launch(UI.class, args);
        UI ui = new UI();
        // I want to create multiple windows (of charts) here, like this:
        Chart c1 = new Chart("c1");
        Chart c2 = new Chart("c2");
    }
}

public class Chart {
    public Chart(String title) {
        Stage stage = new Stage();
        // ...
        stage.show();
    }
}

As you can see, this is a mess, just like my understanding of JavaFX. In fact, the code executes infinitely because of a loop in the Application class.

I'm very confused as to how I can create multiple stages. From what I've understood, by calling the Application.launch method, the Application class then calls the start method in the UI class. This does indeed happen, but as mentioned before, it becomes stuck there. However, I don't actually want to create a stage of type UI, I want to create many Chart stages, but it seems that in order for the application to even begin, I must have a start method.

Can anyone help me get my desired outcome of creating multiple windows?

Upvotes: 0

Views: 2484

Answers (1)

Michael Berry
Michael Berry

Reputation: 72284

Actually, you're not too far off for creating other windows (stages in JavaFX speak):

Stage stage = new Stage();
// ...
stage.show();

This is pretty bang on, you can create a stage, then do whatever (usually involving adding a combination of layout boxes / components to a scene, then setting that scene to the stage), then show it. I wouldn't recommend just blindly showing the stage in the constructor, but that's a bit besides the point.

Despite still carry the JavaFX 2 label, this tutorial is still a good one for getting the basics in this regard.

Two other things look like they're specific points of confusion at present:

  • The stage you get access to in the start() method is, essentially, just a stage created and given to you for convenience. Treat it like any other, though of course you don't need to create it first! (Heck, in many cases you can even ignore it and just create your own stages if that helps you understand things better.)

  • The classic main() method shouldn't actually be called at all in a properly loaded JFX application, so don't do anything in it other than Application.launch(). That's just a fail safe. The real entry point for a JFX application is the start method, so make sure all your logic goes in there (if it seems like nothing in the main method is getting executed, this is why!)

Upvotes: 2

Related Questions