Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
显示ArrayList的值<;列表<;字符串>&燃气轮机;在TableView中(JavaFX)_Javafx_Arraylist_Tableview - Fatal编程技术网

显示ArrayList的值<;列表<;字符串>&燃气轮机;在TableView中(JavaFX)

显示ArrayList的值<;列表<;字符串>&燃气轮机;在TableView中(JavaFX),javafx,arraylist,tableview,Javafx,Arraylist,Tableview,我正在尝试从一个数据集创建一个TableView,该数据集由一个具有字符串值的双嵌套ArrayList组成。不幸的是,我无法在TableView中显示所有值。行没有根据数据集进行编号 互联网的大多数解决方案都表明,我应该使用特定的对象实例。然而,我不是这样工作的。相反,我使用的字符串矩阵大小不定。数据结构如下所示: List<List<String>> values = new ArrayList<List<String>>(); int row

我正在尝试从一个数据集创建一个TableView,该数据集由一个具有字符串值的双嵌套ArrayList组成。不幸的是,我无法在TableView中显示所有值。行没有根据数据集进行编号

互联网的大多数解决方案都表明,我应该使用特定的对象实例。然而,我不是这样工作的。相反,我使用的字符串矩阵大小不定。数据结构如下所示:

List<List<String>> values = new ArrayList<List<String>>();

int rows = 10;
int cols = 4;
             
for(int r=0; r<rows; r++) {
    List<String> line = new ArrayList<String>();
                     
        for(int c=0; c<cols; c++)
            line.add("r: "+r+", c: "+c);
                     
    values.add(line);
}
List values=new ArrayList();
int行=10;
int cols=4;

对于(int r=0;r这就是它的工作原理……我一直受到

@覆盖
public void start(Stage)引发异常{
//创造价值
列表值=新的ArrayList();
int行=10;
int cols=4;
对于(int r=0;r对于(int r=0;r这就是它的工作原理……我一直受到

@覆盖
public void start(Stage)引发异常{
//创造价值
列表值=新的ArrayList();
int行=10;
int cols=4;
对于(int r=0;r对于(int r=0;r给定列的
cellValueFactory
是一个函数,它接受一个包含行(即
列表
)数据的包装器,并生成
可观测值
(例如
属性
),其值将显示在该行和列中

在代码中,每次添加一行时都会更改
cellValueFactory
,因此生成的
cellValueFactory
只是您添加的最后一行中的一行,并且您只能看到最后一行中的数据

您应该为每列设置一次
cellValueFactory
,并且它应该是一个将行数据映射到该列的特定值的函数

例如,以下内容将为您提供所需:

@Override
public void start(Stage stage) throws Exception {

    // create values
    List<List<String>> values = new ArrayList<List<String>>();

    int rows = 10;
    int cols = 4;

    for (int r = 0; r < rows; r++) {
        List<String> line = new ArrayList<String>();

        for (int c = 0; c < cols; c++)
            line.add("r: " + r + ", c: " + c);

        values.add(line);
    }

    // show values in table
    TableView<List<StringProperty>> tableView = new TableView<>();
    ObservableList<List<StringProperty>> data = FXCollections.observableArrayList();

    for (int c = 0; c < cols; c++) {
        TableColumn<List<StringProperty>, String> col = new TableColumn<>("Col: " + c);
        
        final int colIndex = c ;
        col.setCellValueFactory(cd -> cd.getValue().get(colIndex));
        
        tableView.getColumns().add(col);
    }

    for (int r = 0; r < values.size(); r++) {
        List<StringProperty> row = new ArrayList<StringProperty>();

        for (int c = 0; c < values.get(r).size(); c++) {
            row.add(new SimpleStringProperty(values.get(r).get(c)));
        }

        data.add(row);
    }

    tableView.setItems(data);

    stage.setScene(new Scene(tableView, 300, 500));
    stage.show();
}

给定列的
cellValueFactory
是一个函数,它接受一个包含行(即
列表
)数据的包装器,并生成
可观察值
(例如
属性
),其值将显示在该行和列中

在代码中,每次添加一行时都会更改
cellValueFactory
,因此生成的
cellValueFactory
只是您添加的最后一行中的一行,并且您只能看到最后一行中的数据

您应该为每列设置一次
cellValueFactory
,并且它应该是一个将行数据映射到该列的特定值的函数

例如,以下内容将为您提供所需:

@Override
public void start(Stage stage) throws Exception {

    // create values
    List<List<String>> values = new ArrayList<List<String>>();

    int rows = 10;
    int cols = 4;

    for (int r = 0; r < rows; r++) {
        List<String> line = new ArrayList<String>();

        for (int c = 0; c < cols; c++)
            line.add("r: " + r + ", c: " + c);

        values.add(line);
    }

    // show values in table
    TableView<List<StringProperty>> tableView = new TableView<>();
    ObservableList<List<StringProperty>> data = FXCollections.observableArrayList();

    for (int c = 0; c < cols; c++) {
        TableColumn<List<StringProperty>, String> col = new TableColumn<>("Col: " + c);
        
        final int colIndex = c ;
        col.setCellValueFactory(cd -> cd.getValue().get(colIndex));
        
        tableView.getColumns().add(col);
    }

    for (int r = 0; r < values.size(); r++) {
        List<StringProperty> row = new ArrayList<StringProperty>();

        for (int c = 0; c < values.get(r).size(); c++) {
            row.add(new SimpleStringProperty(values.get(r).get(c)));
        }

        data.add(row);
    }

    tableView.setItems(data);

    stage.setScene(new Scene(tableView, 300, 500));
    stage.show();
}

您的“指南”是针对Java 8之前版本的。是Java 8 version.ah super。谢谢您只需执行
final int colIndex=c;
col.setCellValueFactory(cd->new SimpleStringProperty(cd.getValue().get(colIndex));
?您的“指南”是针对Java 8之前版本的。是Java 8 version.ah super吗?谢谢您,只做
final int-colIndex=c;
col.setCellValueFactory(cd->new SimpleStringProperty(cd.getValue().get(colIndex)))有什么不对
?每次添加新行时都会更改工厂。因此,在最后,您只有一个从最后一行生成数据的单元工厂。这是否回答了您的问题?每次添加新行时都会更改工厂。因此,在最后,您只有一个从最后一行生成数据的单元工厂。这是否回答了您的问题?
@Override
public void start(Stage stage) throws Exception {

    // create values
    List<List<String>> values = new ArrayList<List<String>>();

    int rows = 10;
    int cols = 4;

    for (int r = 0; r < rows; r++) {
        List<String> line = new ArrayList<String>();

        for (int c = 0; c < cols; c++)
            line.add("r: " + r + ", c: " + c);

        values.add(line);
    }

    // show values in table
    TableView<List<StringProperty>> tableView = new TableView<>();
    ObservableList<List<StringProperty>> data = FXCollections.observableArrayList();

    for (int c = 0; c < cols; c++) {
        TableColumn<List<StringProperty>, String> col = new TableColumn<>("Col: " + c);
        
        final int colIndex = c ;
        col.setCellValueFactory(cd -> cd.getValue().get(colIndex));
        
        tableView.getColumns().add(col);
    }

    for (int r = 0; r < values.size(); r++) {
        List<StringProperty> row = new ArrayList<StringProperty>();

        for (int c = 0; c < values.get(r).size(); c++) {
            row.add(new SimpleStringProperty(values.get(r).get(c)));
        }

        data.add(row);
    }

    tableView.setItems(data);

    stage.setScene(new Scene(tableView, 300, 500));
    stage.show();
}
@Override
public void start(Stage stage) throws Exception {

    // create values
    List<List<String>> values = new ArrayList<List<String>>();

    int rows = 10;
    int cols = 4;

    for (int r = 0; r < rows; r++) {
        List<String> line = new ArrayList<String>();

        for (int c = 0; c < cols; c++)
            line.add("r: " + r + ", c: " + c);

        values.add(line);
    }

    // show values in table
    TableView<List<String>> tableView = new TableView<>();

    for (int c = 0; c < cols; c++) {
        TableColumn<List<String>, String> col = new TableColumn<>("Col: " + c);
        
        final int colIndex = c ;
        col.setCellValueFactory(cd -> new ObservableValue<String>() {

            // If data are immutable, there's nothing for a listener to do, so we ignore them:
            @Override
            public void addListener(InvalidationListener listener) {}
            @Override
            public void removeListener(InvalidationListener listener) {}
            @Override
            public void addListener(ChangeListener<? super String> listener) {}
            @Override
            public void removeListener(ChangeListener<? super String> listener) {}

            @Override
            public String getValue() {
                return cd.getValue().get(colIndex);
            }
            
        });
        
        tableView.getColumns().add(col);
    }

    

    tableView.setItems(FXCollections.observableList(values));

    stage.setScene(new Scene(tableView, 300, 500));
    stage.show();
}