简体   繁体   English

如何从fxml中的JavaScript内部为javaFX调用java方法

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

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. 我试图找到一种方法从sceneController外部和fxml内部调用sceneController中的方法,而不使用按钮或必须要激活的东西。 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. 现在,imageView有一个fx:id,我在场景控制器内部的方法中使用它来设置图像。 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). 控制imageView的方法必须在场景控制器内部,并且不能从场景控制器外部调用,因为它没有构造函数(试图创建一个并导致问题)。 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. 你可以从fxml内部调用方法(什么按钮和什么不做)我试图调用方法从fxml文件中使用JavaScript设置图像,因为你似乎可以这样做。 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. 或者更好的是知道如何从fxml文件中的JavaScript内部调用java方法。

So I am using JavaFX to build a GUI of for program I am trying to build and am kinda a noob. 所以我使用JavaFX来构建我正在尝试构建的程序的GUI,并且有点像菜鸟。 Right now I am trying to get a nice looking and working GUI before I dive into building the business end of the program. 现在,在我开始构建程序的业务结束之前,我正试图获得一个漂亮的工作GUI。 I am using eclipse 4.6 and Java 8. 我使用的是eclipse 4.6和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 : 所以我希望fxml文件中的这段代码实际上会调用一个名为setBackground的sceneHandeler中的方法,但它只是给我错误:

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

It is not exactly what you asked for, but probably you should declare an initialize() method in your controller. 它并不完全是你要求的,但可能你应该在控制器中声明一个initialize()方法。 If such method exists, the FXMLLoader will execute it on initialization of the controller. 如果存在这样的方法, FXMLLoader将在控制器的初始化时执行它。 This way, you don't need any JavaScript. 这样,您就不需要任何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. 如果要保持方法私有/受保护,则必须使用@FXML对其进行注释。

See also the Controllers section in Introduction to FXML guide. 另请参阅“ FXML简介”中的“ 控制器”部分。

Note: the issues when using a constructor instead of initialize() probably is, that at time of construction, the fields (fi your ImageView) are not yet initialized/injected (meaning they are null ), so you cannot work with them. 注意:使用构造函数而不是initialize()时的问题可能是,在构造时,字段(fi your ImageView)尚未初始化/注入(意味着它们为null ),因此您无法使用它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM