Issue
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.
Solution
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();
}
Answered By - Stevantti
Answer Checked By - Willingham (JavaFixing Volunteer)