如何将数据从Java传递到FXML? [英] How to pass data from Java to FXML?

查看:730
本文介绍了如何将数据从Java传递到FXML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Controller(或者Main或其他文件,我不知道这是如何工作的),我们有以下:

  String foo =Foo。; 

在Scene Bilder生成的FXML中,像这样:

 < Button mnemonicParsing =falseprefHeight =25.0prefWidth =61.0text =Browse/> 

如何使foo的值作为按钮上的文本显示?我应该在哪里存储它,控制器或其他地方?我仍然对主文件,控制器和FXML文件是如何完全一起感到困惑的。

解决方案

您可以通过注入使控制器访问FXML中定义的UI元素。具体来说,在FXML中,给予UI元素 fx:id 属性:

 < Button fx:id =someButtonmnemonicParsing =falseprefHeight =25.0prefWidth =61.0text =Browse/> 

现在在控制器中,定义 @FXML - 未注释字段,其名称与 fx:id 属性值匹配:

  public class Controller {

@FXML
private Button someButton;

}

现在您可以使用任何逻辑配置按钮:

  public class Controller {

@FXML
private Button someButton;

public void initialize(){
String foo =foo;
someButton.setText(foo);
}
}

要回答你的问题的一部分,考虑FXML和控制器作为一对。 FXML定义布局,而控制器定义逻辑(处理用户输入等)。控制器可以使用上述机制访问在FXML文件中定义的UI的元素。



FXMLLoader 加载FXML文件,在默认设置中, FXMLLoader 创建一个控制器类的实例,注入 @FXML $ n $>

code>应用程序子类作为应用程序的起点存在。它通常只是加载一个FXML文件,把FXML的根在场景,并显示在场景初级阶段。如果您有一个更复杂的应用程序,您也可以在这里启动一些服务和后台线程。


In the Controller(or maybe the Main or another file, I'm not sure how all this works), we have the following:

String foo = "Foo.";

And in Scene Bilder-generated FXML, something like this:

<Button mnemonicParsing="false" prefHeight="25.0" prefWidth="61.0" text="Browse" />

How do I make the value of foo appear as the text on the button? And where should I store it, the controller or somewhere else? I'm still perplexed about how do the Main file, the controller(s), and FXML files exactly come together.

解决方案

You give the controller access to the UI elements defined in the FXML by injection. Specifically, in the FXML, give the UI element a fx:id attribute:

<Button fx:id="someButton" mnemonicParsing="false" prefHeight="25.0" prefWidth="61.0" text="Browse" />

Now in your controller, define an @FXML-annotated field with a name that matches the fx:id attribute value:

public class Controller {

    @FXML
    private Button someButton ;

}

Now you can configure the button with whatever logic you need:

public class Controller {

    @FXML
    private Button someButton ;

    public void initialize() {
        String foo = "foo" ;
        someButton.setText(foo);
    }
}

To answer the "how does all this fit together" part of your question, consider the FXML and controller as a pair. The FXML defines the layout, while the controller defines the logic (handling user input, etc). The controller has access to the elements of the UI defined in the FXML file using the mechanism described above.

When an FXMLLoader loads the FXML file, in the default setup, the FXMLLoader creates an instance of your controller class, injects the @FXML-annotated fields into the controller instance, and calls the controller instance's initialize() method.

The Application subclass exists just as the starting point for your application. It will typically just load an FXML file, put the root of the FXML in a Scene and display the Scene in a primary stage. If you have a more complex application, you might also start up some services and background threads here.

这篇关于如何将数据从Java传递到FXML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆