Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 626
lulliezy Avatar asked Jan 12 '14 11:01

lulliezy


2 Answers

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();

FirstStage(){
    x.getChildren().add(openOther);
    this.setScene(new Scene(x, 300, 300));
    this.show();

    openOther.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        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();

SecondStage(){
    y.getChildren().add(x);
    this.setScene(new Scene(y, 300, 300));
    this.show();
   }    
}

And call from main the first stage:

@Override
public void start(Stage primaryStage){
    new FirstClass();
}
like image 111
Stevantti Avatar answered Sep 21 '22 20:09

Stevantti


// My version of the 1st answer above.

package edu.dtcc.michael_javafx_2;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;


public class HelloApplication extends Application {
    public void start(Stage primaryStage) {
        new FirstStage();
    }
}

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

    FirstStage(){
        x.getChildren().add(openOther);
        this.setScene(new Scene(x, 300, 300));
        this.show();

        openOther.setOnAction(t -> new SecondStage());
    }
}

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

    SecondStage(){
        y.getChildren().add(x);
        this.setScene(new Scene(y, 300, 300));
        this.show();
    }
}
like image 32
Eric M Avatar answered Sep 21 '22 20:09

Eric M