Creating multiple stages in JavaFX


I am trying to create multiple stages using different classes, whereby I can have another window being brought up by a click of a button, but this window should be in a different class.

I used to do this in Java where I would create an object of the class in the buttons action and use the name of the object to set the new JFrame visible, but modal to the main JFrame. I tried the same in JavaFX but it failed to work.

I have two different classes and both are in different stages, but I just can't use one stage to display the other stages. I only know to use one class whereby I would create another stage in the action handler method, but this makes the code very long and too complicated.

P.S. what I am trying to accomplish is not multiple screens in the same window. but different windows (stages), and I prefer not to use FXML files, but java files using netbeans.

Any help would be greatly appreciated.

7/28/2014 10:11:59 AM

Accepted Answer

So you want each class to be a sub-class of a Stage. I'll give you two Stages and how to interact with each other.

public class FirstStage extends Stage{
Button openOther = new Button("Open other Stage");
HBox x = new HBox();

    this.setScene(new Scene(x, 300, 300));;

    openOther.setOnAction(new EventHandler<ActionEvent>() {
        public void handle(ActionEvent t) {
            new SecondStage();
        }//end action

For the second Stage,

public class SecondStage extends Stage {
Label x = new Label("Second stage");
VBox y = new VBox();

    this.setScene(new Scene(y, 300, 300));;

And call from main the first stage:

public void start(Stage primaryStage){
    new FirstClass();
1/12/2014 11:18:43 PM

<AnchorPane prefHeight="200.0" prefWidth="200.0" />
<Button layoutX="264.0" layoutY="226.0" mnemonicParsing="false" text="Save" />
<Label layoutX="200.0" layoutY="144.0" text="Name" />
<Label layoutX="200.0" layoutY="192.0" text="Password" />
<TextField layoutX="264.0" layoutY="140.0" />
<TextField layoutX="264.0" layoutY="188.0" />

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow