在FXML中创建动态数量的组件 [英] Creating dynamic amount of components in FXML

查看:485
本文介绍了在FXML中创建动态数量的组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个可以帮助你学习JavaFX的笔记本卡程序。它通过XML和启动来保存类,它找到XML文件,并将它们添加到名为NoteCardSet的ArrayList的ArrayList,这是NoteCard的ArrayList。有了这个,我做了一个动态的按钮,把它们放在4列宽。这是代码:

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

(NoteCardSet noteCardSet:allProjects){

按钮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 ++;
}
}

显然这是JavaFX中可创建的,但是可以在FXML中创建了这个?

解决方案

不,你不能在FXML中这样做。在fxml中无法写入 LOOP 。如果你只是考虑一个 Button ,那么你可以使用 SceneBuilder 并拖放多个按钮。



尽管如果考虑到更复杂的UI,并想重复,您可以创建一个单独的FXML,并使用 < fx:include>



您还可以使用循环多次加载相同的fxml,并将所有相关数据放在initialize()中,但这可能不是您要查找的最佳解决方案。


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?

解决方案

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.

这篇关于在FXML中创建动态数量的组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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