I want to pop a new window up by clicking a button. since I am using scene builder, my project has main.java and controller.java. and i have two fxml files named Interface.fxml(which primarily appears) and Popup.fxml.
I searched a lot but all the questions and answers were about doing it in main.java not in controller.java. I read about referencing stage and scene in other class but I have failed in implementing.
Here is what i have wrote.
import ...
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
Parent root = FXMLLoader.load(getClass().getResource("Interface.fxml"));
Scene scene = new Scene(root);
primaryStage.setTitle("Calculator");
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
code above is main.java and
@FXML
private void onButtonClicked(ActionEvent ae) {
try {
FXMLLoader loader=new FXMLLoader(getClass().getResource("Popup.fxml"));
loader.setRoot(this);
loader.setController(this);
loader.load();
} catch(Exception e) {
e.printStackTrace();
}
}
Code above is in controller.java.
I want to know how to create a new window in a controller.java. Please help me out! (if additional part is needed, please let me know.)