SpringLearner
SpringLearner

Reputation: 13844

how javafx stage creation is done

I am just a beginner in javafx.I was just seeing some examples in ensemble.jar and has a doubt in the following program.Here in this there are 2 methods start and init both of which accepts arguments of type Stage. init() is called from start().My doubt is stage decoration(adding group,progressindicator,gridpane) is done in start method.So primaryStage.show() will display the decorated stage but here if I write primaryStage1.show() in the start() then also decorated stage is also displaying.I want to know how

package fx;

/**
 * Copyright (c) 2008, 2012 Oracle and/or its affiliates.
 * All rights reserved. Use is subject to license terms.
 */
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.layout.GridPane;

/**
 * A sample that demonstrates the Progress Indicator control in various modes.
 *
 * @see javafx.scene.control.ProgressIndicator
 * @related controls/ProgressBar
 */
public class ProgressIndicatorSample extends Application {

    private void init(Stage primaryStage) {
        Group root = new Group();
        primaryStage.setResizable(false);
        primaryStage.setScene(new Scene(root, 400,400));

        GridPane g = new GridPane();

        ProgressIndicator p1 = new ProgressIndicator();
        p1.setPrefSize(50, 50);

        ProgressIndicator p2 = new ProgressIndicator();
        p2.setPrefSize(50, 50);
        p2.setProgress(0.25F);

        ProgressIndicator p3 = new ProgressIndicator();
        p3.setPrefSize(50, 50);
        p3.setProgress(0.5F);

        ProgressIndicator p4 = new ProgressIndicator();
        p4.setPrefSize(50, 50);
        p4.setProgress(1.0F);

        g.add(p1, 1, 0);
        g.add(p2, 0, 1);
        g.add(p3, 1, 1);
        g.add(p4, 2, 1);
char x[]={'a','m'};
x.toString();
System.out.println(x);
        g.setHgap(40);
        g.setVgap(40);

        root.getChildren().add(g);

    }

    public double getSampleWidth() { return 400; }

    public double getSampleHeight() { return 400; }

    @Override public void start(Stage primaryStage1) throws Exception {
        init(primaryStage1);
        primaryStage1.show();
    }
    public static void main(String[] args) { launch(args); }
}

Upvotes: 1

Views: 1222

Answers (1)

jewelsea
jewelsea

Reputation: 159331

On application stage creation

On launching an application, the JavaFX system creates an initial stage for the system and, without showing it, passes a reference to this stage object to the application's start method.

Explaining what happens in your sample program

There is only one stage involved here.

When you launch a JavaFX application (as you do in your main method), the JavaFX toolkit system is started. The JavaFX system creates an initial stage (window) for your application, but does not show it, instead it passes a reference to that stage to your start method (which you accept as the parameter primaryStage1). You than pass that stage reference to the init method, which places all of the content (groups, progress bars and so forth) into a scene which it places on the stage. Finally, execution returns to your start method and you request that the stage be shown (which tells the JavaFX system to display the stage on the screen and render the content inside the stage).

In addition to the initial stage created by the JavaFX system and passed into your program, you could have created more new stages in your program so that you have additional stages (windows) for pop-up dialog boxes or other reasons, but you did not.

On creating additional stages

To create more new stages, you can do something like the following (from Sergey's answer to How to create and show common dialog (Error, Warning, Confirmation) in JavaFX 2.0?)

Stage dialogStage = new Stage();
dialogStage.initModality(Modality.WINDOW_MODAL);
dialogStage.setScene(new Scene(VBoxBuilder.create().
    children(new Text("Hi"), new Button("Ok.")).
    alignment(Pos.CENTER).padding(new Insets(5)).build()));
dialogStage.show();

On object reference passing

See also: Is Java "pass-by-reference" or "pass-by-value"? to understand how the stage is being passed around in your program.

Upvotes: 4

Related Questions