将数据添加到列的简单方法 [英] Simple way to add data to columns

查看:120
本文介绍了将数据添加到列的简单方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在JavaFX TableView中向列中插入两个单独的数据集。基本上我有2个带有字符串的LinkedLists,我想把一个列表放在一列中,另一个列表放在第二列中。

I want to insert two separate datasets to columns in JavaFX TableView. Basically I have 2 LinkedLists with Strings and I want to put one list in one column and another to second one.

最简单的方法是什么?或者是另一个更好的JavaFX元素?

What is the easiest way to do this? Or is another JavaFX element better for this?

到目前为止,我找到的解决方案只基于插入行的集合,这不是我想要做的。 / p>

Only solutions I found so far were based on collections that were inserted into rows, which is not what I want to do.

推荐答案

我强烈建议您重新组织数据,以便拥有一个包含两个属性的类,其中一个是您的元素第一个列表,另一个是第二个列表中的相应元素。然后你可以列出该类的对象列表,并且你可以减少到通常的情况。

I would strongly recommend reorganizing your data, so that you have a class with two properties, one of which is an element from your first list, and the other of which is the corresponding element from your second list. Then you can make a list of objects of that class, and you are reduced to the usual case.

例如。假设您有

// your first list:
List<Integer> intList = ... ;
// your second list:
List<String> stringList = ... ;

然后定义

public class MyDataType {
    private final int intValue ;
    private final String stringValue ;
    public MyDataType(int intValue, String stringValue) {
        this.intValue = intValue ;
        this.stringValue = stringValue ;
    }
    public int getIntValue() {
        return intValue ;
    }
    public String getStringValue() {
        return stringValue ;
    }
}

现在你可以做到

TableView<MyDataType> table = new TableView<>();
for (int i = 0; i < intList.size() && i < stringList.size(); i++) {
    table.getItems().add(new MyDataType(intList.get(i), stringList.get(i)));
}

并按常规方式定义列。

如果由于某种原因确实无法进行数据转换,那么可以将表的类型视为整数,每行的值是两个列表的索引。使用值 0,1,... 填充表,最大为列表大小。这看起来像:

If you really can't make that data transformation for some reason, then you can regard the type of the table as an integer, with the value for each row being the index into the two lists. Populate the table with the values 0, 1,... up to the size of the lists. This looks like:

import java.util.Arrays;
import java.util.List;

import javafx.application.Application;
import javafx.beans.property.ReadOnlyIntegerWrapper;
import javafx.beans.property.ReadOnlyStringWrapper;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class TableViewByColumns extends Application {

    @Override
    public void start(Stage primaryStage) {
        List<Integer> intValues = Arrays.asList(1, 2, 3, 4, 5);
        List<String> stringValues = Arrays.asList("One", "Two", "Three", "Four", "Five");

        TableView<Integer> table = new TableView<>();
        for (int i = 0; i < intValues.size() && i < stringValues.size(); i++) {
            table.getItems().add(i);
        }

        TableColumn<Integer, Number> intColumn = new TableColumn<>("Value");
        intColumn.setCellValueFactory(cellData -> {
            Integer rowIndex = cellData.getValue();
            return new ReadOnlyIntegerWrapper(intValues.get(rowIndex));
        });

        TableColumn<Integer, String> nameColumn = new TableColumn<>("Name");
        nameColumn.setCellValueFactory(cellData -> {
            Integer rowIndex = cellData.getValue();
            return new ReadOnlyStringWrapper(stringValues.get(rowIndex));
        });

        table.getColumns().add(intColumn);
        table.getColumns().add(nameColumn);

        primaryStage.setScene(new Scene(new BorderPane(table), 600, 600));
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

这篇关于将数据添加到列的简单方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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