如何从javaFX中的String []列表中填充TableView? [英] How to populate TableView from list of String[] in javaFX?

查看:150
本文介绍了如何从javaFX中的String []列表中填充TableView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用列表中的字符串数组动态填充TableView(我猜有更好的方法来处理信息,但它是我给予的...),所以我创建一个tableView,创建所有的列和东西,但是在填充tableview的那一刻我迷失了。

I'm trying to dynamically populate a TableView with arrays of strings taht I have inside of a list (I guess there are better ways to handle information, but its is what I was given to do...), so I create a tableView, create all the columns and stuff, but I'm lost at the moment of populating the tableview.

列表中包含所有的字符串数组,似乎一切为了工作,没有任何数组或任何类似的错位数据,当我运行项目时,列名称显示为我想要的,并且在我的列表上的每个数组的tableview上有一行,但我只是无法让它显示信息。

The list has all the string arrays inside and everything seems to work, there is no missplaced data on any array or anything like that, and when I run the project, the column names appear as I wanted them to, and there is a row on the tableview for every array on my list, but I just can't get it to display the information.

我想我错过了将列表中的信息设置到单元格中的语句,但我不能看到这样做的方式。任何帮助将在cookie中的重量感谢:0

I guess I'm missing the statement to set the information from the list into the cells, but I can't see the way to do it. Any help will be thanked in its weight in cookies :0

这是我目前使用的过程代码。

Here's the code of the process im using so far.

int tam = regs.length-3;
String[] aux = new String[regs.length-3];
ObservableList<String[]> list = FXCollections.observableArrayList();

for (int i = 0; i < db.length; i++) {            
    aux[(i % tam)] = db[i];
    if (((i % tam) == tam-1) & (i > 0)) {
        list.add(aux);
        aux = new String[tam];
    }
}

TableView<String[]> table = new TableView<>();

TableColumn[] tableColumns = new TableColumn[tam];
for (int i = 3; i < regs.length; i++) {
    tableColumns[i-3] = new TableColumn(regs[i]);
}
table.getColumns().addAll(tableColumns);

table.setItems(list);


推荐答案

您需要在表格列上设置单元格值工厂。类似

You need to set cell value factories on your table columns. Something like

TableColumn[] tableColumns = new TableColumn[tam];
for (int i = 3; i < regs.length; i++) {
    tableColumns[i-3] = new TableColumn(regs[i]);
    final int index = i - 3 ; // I think, don't really understand what you're doing with indexes...
    tableColumns[i-3].setCellValueFactory(new Callback<CellDataFeatures<String[], String>>, ObservableValue<String>>) {
        @Override
        public ObservableValue<String> call(CellDataFeatures<String[], String> cellData) {
            return new ReadOnlyStringWrapper(cellData.getValue()[index]);
        }
    });
}

这篇关于如何从javaFX中的String []列表中填充TableView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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