构建动态“行”的JavaFX体系结构 [英] JavaFX architecture in building dynamic "rows"

查看:110
本文介绍了构建动态“行”的JavaFX体系结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正在构建的应用程序,其中包含一个表,我没有使用tableview来构建此表,因为我需要每行能够扩展类似于手风琴。我能够通过使用时间轴并循环遍历数据并构建每一行来实现我所需要的(现在它的原始类型,因为我仍在处理虚拟数据,最终它将是一个列表迭代器而不仅仅是for循环)但我对它的完成情况不满意。有很多默认值永远不会改变所以我不需要每次都在我的worker类中设置它们,我决定只将它们添加到我放在一起的对象类中。所以基本上,在高级别它看起来像这样:

I have an application that I am building that has a table in it, I'm not using a tableview to build this table because I need each row to be able to expand similar to an accordion. I was able to achieve what I need by using a timeline and looping through the data and building each row (its kind of crude right now since I'm still working with dummy data eventually it will be a list iterator and not just a for loop) but I'm not happy with how its done. There are a lot of default values that will never change so I don't really need to set them in my worker class every time, I decided to just add them to the object class that I put together. So basically, at a high level it looks something like this:

for (int i = 0; i < 30; i++) {
    RowBuilder builder = new RowBuilder(tableBox, i);
    try {
    builder.run();
} catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

}

我正在传递父母这是一个VBox - tableBox,然后我传递计数供以后使用。

I'm passing it the parent which is a VBox - tableBox, then I'm passing the count for later use.

在RowBuilder中我得到一个对象DashboardRow的新实例,其中设置了所有默认值,然后我为每一行设置数据并返回DashboardRow。

Inside the RowBuilder I'm getting a new instance of the object DashboardRow which has all the defaults set in it, then I'm setting the data for each row and returning the DashboardRow.

以下是DashboardRow中getter设置值的示例

Here is an example of a getter setting values in the DashboardRow

public HBox getMainRow() {
    mainRow.setAlignment(Pos.CENTER_LEFT);
    mainRow.setPrefHeight(60);
    mainRow.setMinHeight(60);
    mainRow.setMaxHeight(60);
    mainRow.setPrefWidth(-1);
    mainRow.setStyle("-fx-background-color:#FFFFFF;");
    return mainRow;
}

在DashboardRow类中,我为每个元素创建了大量新对象我需要排好队。每行有21个,主要是VBox,HBox和StackPane来构建实际的行,其余的只是标签和按钮。

Inside the DashboardRow class I have a ton of new objects being created for every element I need in the row. There are 21 for each row, mostly VBox, HBox and StackPane to build the actual row, the rest are just labels and buttons.

这是到目前为止的样子。已开启和已关闭的州。

This is what is looks like so far. Opened and closed states.



有没有更好的方法在javafx中动态构建这样的东西?我基本上从数据库中提取数据并循环遍历该数据以填充行。


Is there a better way to dynamically build things like this in javafx? I'm basically pulling data from a database and looping through that data to populate a row.

推荐答案

我无法评论,但无论如何它可能是一个答案。为什么不能使用自定义表格单元格的setGraphic方法并将accordion节点放在表格中。 setGraphic接受任何类型的节点。

I can't comment but it may be an answer anyway. Why can't you use the setGraphic method of a custom table cell and put the accordion node in a table. setGraphic takes any kind of node.

这听起来比你正在做的更简单。

It sounds simpler than what you're doing.

我刚试过它与 Oracle示例而且效果很好。
我添加了

I just tried it out with the Oracle sample and it works great. I added

    Callback<TableColumn<Person, String>, TableCell<Person, String>> accCellFactory
            = new Callback<TableColumn<Person, String>, TableCell<Person, String>>() {
                @Override
                public TableCell call(TableColumn p) {
                    TitledPane t1 = new TitledPane("T1", new Button("B1"));
                    TitledPane t2 = new TitledPane("T2", new Button("B2"));
                    TitledPane t3 = new TitledPane("T3", new Button("B3"));
                    Accordion accordion = new Accordion();
                    accordion.getPanes().addAll(t1, t2, t3);
                    TableCell tc = new TableCell();
                    tc.setGraphic(accordion);
                    return tc;
                }
            };

并更改此行 firstNameCol.setCellFactory(accCellFactory);

我得到了

当然你可能需要除按钮以外的其他东西,但我只是从javadoc复制了Accordion样本。

Of course you might want something other than buttons but I just copied the Accordion sample from the javadoc.

这篇关于构建动态“行”的JavaFX体系结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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