This is also my first post. It's also my first project using JavaFX. I am trying to call a small popup window (by invoking some kind of Key like F9 for example) which would have a TableView from a Textfield on the Main Window. For now, I am trying to invoke the Popup Window from a button on the Main Window. I of course do want to return the value from the TableView back to the Main Window but that comes later. I am having issues even calling my popup window.
I have looked at various examples and solutions on here already but have not been able to resolve my issue. I will eventually land up having 3 popup windows being called from my Main Window.
I have the following files under my MVC architecture:
- Main.java -- (src/sample folder)
- StudentController.java -- (src/sample/controller folder)
- StudentDAO.java and sexDAO.java (Data Access Object) -- (src/sample/model folder)
- Student.java (public class Student and constructor) -- (src/sample/model folder)
- DBUtil under util for the OJDBC -- (src/sample/util folder)
- FXML files created with Scene Builder -- (src/sample/view folder)
- RootLayout.fxml
- StudentView.fxml (Main Window)
- GenderPopup.fxml (Popup Window with TableView showing records)
Main.java
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import java.io.IOException;
public class Main extends Application {
private Stage primaryStage;
private BorderPane rootLayout;
@Override
public void start(Stage primaryStage) {
this.primaryStage = primaryStage;
this.primaryStage.setTitle("Sample JavaFX App");
initRootLayout(); // Initialize RootLayout
showStudentView();
}
//Initializes the root layout.
public void initRootLayout() {
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("view/RootLayout.fxml"));
rootLayout = (BorderPane) loader.load();
Scene scene = new Scene(rootLayout);
primaryStage.setScene(scene);
primaryStage.show(); //Display the primary stage
} catch (IOException e) {
e.printStackTrace();
}
}
//Shows the Patient inside the root layout.
public void showStudentView() {
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("view/StudentView.fxml"));
AnchorPane PatientView = (AnchorPane) loader.load();
rootLayout.setCenter(PatientView);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
StudentController.java
package sample.controller;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class StudentController {
@FXML private TextField studentIdText;
@FXML private TextField lastNameText;
@FXML private TextField firstNameText;
@FXML private TextField sexText;
@FXML private Button popupButton;
@FXML
private void initialize () {
// I have additional listeners here doing different things that I have excluded which do pretty much similar things as shown below
sexText.focusedProperty().addListener((arg0, oldPropertyValue, newPropertyValue) -> {
if (!newPropertyValue) {
try {
searchSexDescription();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
});
}
// Function to Search Sex Description
@FXML
private void searchSexDescription() throws ClassNotFoundException, SQLException {
try {
if (!sexText.getText().isEmpty()) {
// Get Gender Description
Patient sex = SexDAO.searchSex(sexText.getText());
populateAndShowSexDescription(sex);
}
} catch (SQLException e) {
System.out.println("Exception raised in searchSexDescription");
e.printStackTrace();
throw e;
}
}
@FXML
public void showGenderPopup() throws IOException {
try {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("view/GenderPopup.fxml"));
Parent root = (Parent) fxmlLoader.load();
Scene scene = new Scene(root);
Stage stage = new Stage();
stage.setScene(scene);
stage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
StudentView.fxml
This is the Main Window which has a button which onAction is calling showGenderPopup()
So, at this stage, my Student Record is fetching nicely on the Main Window. However, when I press the Popup button to try to invoke the Popup window, I am getting an error (Location is not set). Now, I know that it's not an error with the filename which does occur with the similar error. My path is correct and so is the Filename. I am guessing that somehow the Popup Window being the child is not being able to find the Parent. Any help is greatly appreciated!
Error: Caused by: java.lang.IllegalStateException: Location is not set.
Thanks