0

I'm trying to create custom-popup via JavaFX, and have some trouble with initiating it from a static method.

How can I initiate a new window from a static method?

General information about my program - The user should type data and select/deselect a checkbox. Pressing on "submit" button runs a static method that does some stuff, and according to the user checkbox selection - run another method that does other stuff.

If the checkbox is deselected, I would like to open another window (custom popup).

However, I can't do that, since all my methods are static (can't change that). The method uploadCustomIndexWindow is defined as static, and therefore, when I try to initiate my custom-popup, I get the error

Cannot make a static reference to the non-static method getClass() from the type Object.

.

private static Index getStartEndIndex(String childFormat, boolean isFromExportTDP) {

    if(IndexMap.getIndexMap().get(childFormat) == null) {

        Index index;
        if (isFromExportTDP) {
            if(childFormat.equalsIgnoreCase("pdf")){
                index = new Index(childFormat, 2, 12);
            }
            else {
                index = new Index(childFormat, 2, 5);
            }
        }
        else{
            // Custom pop-up
            uploadCustomIndexWindow();

            index = new Index(childFormat, startIndex, endIndex);
        }
        IndexMap.getIndexMap().put(childFormat, index);
    }
    return IndexMap.getIndexMap().get(childFormat);
}



public static void uploadCustomIndexWindow() throws IOException{
    FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("CustomIndexScreen.fxml"));
    Parent root1 = (Parent) fxmlLoader.load();
    Stage stage = new Stage();
    stage.setScene(new Scene(root1));
    stage.setTitle("Custom Index Screen");
    stage.show();
}
szk
  • 35
  • 4

1 Answers1

0

please use "YourClassName.class" instead of "getClass()" !

Sunil
  • 54
  • 4
  • I recieve the following exception - javafx.fxml.LoadException: ..........CustomIndexScreen.fxml at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2543) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441) at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409) – szk Aug 25 '19 at 08:45
  • What do the `Caused by:`s say? @ShirZabolotnyKlein (and consider asking a new question, if one doesn't already exist for the problem you're now facing) – Slaw Aug 25 '19 at 08:48
  • May be "CustomIndexScreen.fxml" path is incorrect – Sunil Aug 25 '19 at 08:54
  • No, that cannot be the case. Otherwise there's no way the `FXMLLoader` would know about the url of the `.fxml` file. Stacktrace contains the url though. For resources not found `getResource` would simply return `null`. – fabian Aug 25 '19 at 09:57