JavaFX:如何调用fxml文件并将数据加载到其中 [英] JavaFX : How to call the fxml files and load data into it

查看:854
本文介绍了JavaFX:如何调用fxml文件并将数据加载到其中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是JavaFX的新手,我正在开发一个JavafX项目,我希望将数据从网络加载到 GridPane 。我试图弄清楚如何添加我在UI中从网络收到的数据。

I am pretty new to JavaFX and I am working on a JavafX project in which I would like to load data from network into a GridPane. I am trying to figure out how I can add data which I received from the network in UI.

我已经创建了一个gridpane,只有在窗格中加载的东西是Name,Image。

I already have a gridpane created, and only things to load in the Pane are Name, Image.

我检查了一些资源,但鉴于我对JavaFX的理解有限,不知道在窗格中添加数据需要修改什么。

I checked some resources, but given my limited understanding in JavaFX, didn't knew what to modify for adding data in pane.

我有以下代码:

public class AccountController {


    fetchCanvasImagesFromServer.setOnSucceeded(new EventHandler<WorkerStateEvent>() {
            @Override
            public void handle(WorkerStateEvent event) {
                imageObservableList = FXCollections.observableList(fetchCanvasImagesFromServer.getValue());

                for(RestImage image : imageObservableList){
                    if(!(image == null)) {
                        if (!(image.getCanvasImage() == null)) {
                            try {
                                InputStream in = new ByteArrayInputStream(image.getCanvasImage());
                                BufferedImage bImageFromConvert = ImageIO.read(in);
                                Image fxImage = SwingFXUtils.toFXImage(bImageFromConvert,null);

                                if(!(fxImage == null)){
                                    System.out.println("Fx image is not null");
                                }
                            }catch (Exception ignored){}


                        }
                    }
                }
            }
        });
}

这里我有来自Canvas的数据。现在,这个代码由LoginController调用,并且只有在登录后才会检索此数据。

Here I have the data from Canvas. Now, this code is called by LoginController, and this data is retrieved only after login.

public class LoginController {

    @FXML
    private TextField user;
    @FXML private TextField password;
    @FXML private Button loginButton;


    public void initManager(final LoginManager loginManager) {
        loginButton.setOnAction(event -> {
            String sessionID1 = authorize();
            if (sessionID1 != null) {
                loginManager.authenticated();
            }
        });
    }
}

最后,上面经过验证的方法调用网络方法检索数据。

And finally, the authenticated method above calls the network methods to retrieve data.

Gridpane:

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.*?>

<GridPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity"
          prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1"
        fx:controller="Controller.AccountController">
    <columnConstraints>
        <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
        <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
    </columnConstraints>
    <rowConstraints>
        <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
        <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
        <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
    </rowConstraints>
</GridPane>

我的问题是,如何在画布中加载RestImage对象数据。 FXML 。请告诉我。

My problem is, how can I load the RestImage object data in canvas.fxml. Kindly let me know.

推荐答案

加载 canvas.fxml 后,你不要修改 FXML 但是改为使用UI对象。如果您将 fx:id =root添加到 FXML GridPane c $ c>,然后你可以在 AccountController 中有一个字段 @FXML GridPane root; 。加载图像后,为每个图像创建一个 ImageView 并附加到根目录:

Once canvas.fxml is loaded you do not modify the FXML but the UI objects instead. If you add fx:id="root" to the GridPane in FXML, you can then in AccountController have a field @FXML GridPane root;. Once your images are loaded, for each image create an ImageView and attach to the root:

root.add(myImageView, columnNumber, rowNumber);

整体应用程序设计值得怀疑,但上述内容将为您提供特定问题的解决方案。

The overall application design is questionable, but the above will give you a solution for your particular question.

这篇关于JavaFX:如何调用fxml文件并将数据加载到其中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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