0

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:

  1. Main.java -- (src/sample folder)
  2. StudentController.java -- (src/sample/controller folder)
  3. StudentDAO.java and sexDAO.java (Data Access Object) -- (src/sample/model folder)
  4. Student.java (public class Student and constructor) -- (src/sample/model folder)
  5. DBUtil under util for the OJDBC -- (src/sample/util folder)
  6. FXML files created with Scene Builder -- (src/sample/view folder)
    1. RootLayout.fxml
    2. StudentView.fxml (Main Window)
    3. 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

Roxyrollers
  • 13
  • 1
  • 6
  • Does this answer your question? [How do I determine the correct path for FXML files, CSS files, Images, and other resources needed by my JavaFX Application?](https://stackoverflow.com/questions/61531317/how-do-i-determine-the-correct-path-for-fxml-files-css-files-images-and-other) – Slaw Nov 16 '20 at 09:32
  • ^ linking duplicate (that was posted after this one) because it goes into more detail – Slaw Nov 16 '20 at 09:32

1 Answers1

1

Your path is not correct. You're trying to get the FXML resource from your StudentController class, not your Main class. As you mentioned, your project structure includes:

  • /sample/Main.java
  • /sample/controller/StudentController.java
  • /sample/view/GenderPopup.fxml

Since you call getClass().getResource(...) inside StudentController a path without a leading / is relative to the location of StudentController. This means the path is ultimately resolved to:

  • /sample/controller/view/GenderPopup.fxml

Which doesn't exist.

You need to use /sample/view/GenderPopup.fxml as the path if using StudentController.class to query the resource.

Slaw
  • 37,820
  • 8
  • 53
  • 80