如何使用 Arraylist 在 JavaFX 中填充 TableView?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18402685/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 01:16:42  来源:igfitidea点击:

How can I populate a TableView in JavaFX by using Arraylist?

javajavafx

提问by Rajesh

I want to populate table view in javafx by using array list .I do not want to use any model .i want to make array list as my source of data for populating tableview .

我想通过使用数组列表在 javafx 中填充表格视图。我不想使用任何模型。我想将数组列表作为我填充表格视图的数据源。

Code:


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

    TableView table_view = new TableView<>(names);

    firstDataColumn.setCellFactory(?????);/// here the problem comes what is the cell factory in case of arraylist      

回答by Diego Urenia

You must use a ObservableListas your datasource and to achieve this when you have a Listyou can use:

您必须使用 aObservableList作为您的数据源,并在您拥有List可以使用的a 时实现这一点:

FXCollections.observableList(yourList);

回答by Uluk Biy

If there is no model then you want to show only one column list. So after taking assumption of that your arraylist's object type is the wrapper class (of the primitive data types) like Integer, Double, String etc., you can use ListViewinstead of TableView.

如果没有模型,那么您只想显示一列列表。因此,在假设您的数组列表的对象类型是(原始数据类型的)包装类(如 Integer、Double、String 等)之后,您可以使用ListView而不是 TableView。

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