Creating dynamic amount of components in FXML

我只是一个虾纸丫 提交于 2019-11-28 12:57:33

问题


I made a note card program that can help you study with JavaFX. It saves the class through XML and on boot up, it finds the XML files and adds them to an ArrayList called allProjects of type NoteCardSet, an ArrayList of NoteCards. With this, I made a dynamic amount of buttons that puts them 4 columns wide. Here is the code for that:

    int amountPerRow = 4;
    int current = 0;
    int row = 0;

    for (NoteCardSet noteCardSet : allProjects) {

        Button b = new Button(noteCardSet.getName());

        GridPane.setConstraints(b, current, row);
        centerMenu.getChildren().add(b);

        b.setOnAction(e -> {

            border.setCenter(noteCardSetLayout(noteCardSet));
        });

        if (current < amountPerRow - 1)
        {
            current++;
        }
        else if (current >= amountPerRow - 1)
        {
            current = 0;
            row++;
        }
    }

Obviously this is creatable in JavaFX but is it possible to created this in FXML?


回答1:


No you cannot do this in FXML. There is no way to write a LOOP in fxml. If you are just considered about a Button, then you may use SceneBuilder and drag-drop multiple buttons.

Though, if you are considered about a more complex UI and want to repeat them, you can create a separate FXML and include it as many time as you need using <fx:include>.

You can also load the same fxml multiple times using a loop and put all the concerned data inside the initialize(), but this might not be the best solution you are looking for.



来源:https://stackoverflow.com/questions/30471827/creating-dynamic-amount-of-components-in-fxml

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