简体   繁体   English

如何在Javafx中用Arraylist填充表列

[英]How to fill a tablecolumn with an Arraylist in Javafx

I m trying to fill a table column with a String ArrayList. 我正在尝试用String ArrayList填充表列。 Normally i know how to fill it with object attributes but in my case i just need to fill it with Strings from an ArrayList. 通常我知道如何用对象属性填充它,但就我而言,我只需要用ArrayList中的字符串填充它即可。 How can i do it ? 我该怎么做 ?

through the answer which i got still it doesnt seems to work :( 通过我仍然得到的答案,它似乎不起作用:(

table = new TableView(FXCollections.observableArrayList(id));

            linkC.setCellValueFactory((p) -> {
                return new ReadOnlyStringWrapper(p.getValue());
            });

It's possible, it just may not be the best idea. 有可能,这可能不是最好的主意。 Tables are designed to be used with a class of data. 表被设计为与一类数据一起使用。 A single column table is just a list. 单列表只是一个列表。

import java.util.ArrayList;
import java.util.Arrays;
import javafx.application.Application;
import javafx.beans.property.ReadOnlyStringWrapper;
import javafx.collections.FXCollections;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.stage.Stage;

public class TableTest extends Application {
    public static void main(String[] args) { launch(args); }

    @Override
    public void start(Stage stage) {
        TableView<String> tv = new TableView(FXCollections.observableArrayList(
                new ArrayList<String>(Arrays.asList(new String[]{
                    "a","bb","ccc","dddd","eeeee"
                }))));
        TableColumn<String, String> tc = new TableColumn<>("string");
        tc.setCellValueFactory((p) -> {
            return new ReadOnlyStringWrapper(p.getValue());
        });
        tv.getColumns().add(tc);
        stage.setScene(new Scene(tv));
        stage.show();
    }

}

example below: 下面的例子:

List<Double> doubles = new ArrayList<Double>();
doubles.add(12.12d);
ObservableList<Double> names = FXCollections.observableArrayList(doubles);
ListView<Double> listView = new ListView<Double>(names);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM