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();
}
0

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

4 Comments

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)
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)
May be "CustomIndexScreen.fxml" path is incorrect
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.