JavaFX : How to call the fxml files and load data into it

岁酱吖の 提交于 2020-01-03 05:16:08

问题


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.

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

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

Code I have :

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){}


                        }
                    }
                }
            }
        });
}

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>

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


回答1:


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.



来源:https://stackoverflow.com/questions/35868721/javafx-how-to-call-the-fxml-files-and-load-data-into-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!