How to call a java method from inside JavaScript inside fxml for javaFX

Fibonacci :

I am trying to find a way to call a method in the sceneController from outside the sceneController and inside the fxml without using a button or something that has to be done to activate. I want it to run once everytime the application starts up. So currently, every time the program starts it creates an image view that covers the entire screen. However everyone the program opens I want a random image from a folder appear. Right now the imageView has an fx:id that I use in a method inside the scene controller to then set the image. The method inside the scene controller works perfectly. However the problem is that I want the image to be randomly generated at the programs startup and not when you hit a button (what happens currently). The method to control the imageView must be inside the scene controller and cannot be called from outside the scene controller as it has no constructor (tried to create one and it caused issues). Becauase you can call methods from inside the fxml (what buttons and what not do) I am trying to call the method to set the image from inside the fxml file using JavaScript as it seems like you can do it like that. I was just wondering if anyone knew a better solution to this problem. Or better yet know how to call a java method from inside JavaScript inside a fxml file.

So I am using JavaFX to build a GUI of for program I am trying to build and am kinda a noob. Right now I am trying to get a nice looking and working GUI before I dive into building the business end of the program. I am using eclipse 4.6 and Java 8.

<VBox xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.initSceneController" >

        <fx:script>
            var executeOnce = false;

            if(executeOnce === false) {
                #setBackground;
                executeOnce = true;
            }

            else {
            }


        </fx:script>

So I was hoping this code in the fxml file would actually call a method inside the sceneHandeler called setBackground but its just giving me the error :

<eval>:5:5 Expected an operand but found error
                #setBackground;
                ^ in <eval> at line number 5 at column number 5
Archangel1C :

It is not exactly what you asked for, but probably you should declare an initialize() method in your controller. If such method exists, the FXMLLoader will execute it on initialization of the controller. This way, you don't need any JavaScript.

public void initialize() {
    // choose your random image here
    // imageView.setImage(...);
}

If you want to keep the method private/protected, you have to annotate it with @FXML.

See also the Controllers section in Introduction to FXML guide.

Note: the issues when using a constructor instead of initialize() probably is, that at time of construction, the fields (f.i. your ImageView) are not yet initialized/injected (meaning they are null), so you cannot work with them.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=126753&siteId=1