Java 向列添加数据的简单方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31318713/
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
Simple way to add data to columns
提问by tikend
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 TableView 的列中插入两个单独的数据集。基本上我有 2 个带有字符串的 LinkedLists,我想将一个列表放在一列中,另一个放在第二列中。
What is the easiest way to do this? Or is another JavaFX element better for this?
什么是最简单的方法来做到这一点?或者另一个 JavaFX 元素更适合这个?
Only solutions I found so far were based on collections that were inserted into rows, which is not what I want to do.
到目前为止,我发现的唯一解决方案是基于插入行的集合,这不是我想要做的。
采纳答案by James_D
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.
我强烈建议重新组织您的数据,以便您拥有一个具有两个属性的类,其中一个是第一个列表中的元素,另一个是第二个列表中的相应元素。然后,您可以列出该类的对象,然后您就可以简化为通常的情况。
E.g. suppose you have
例如假设你有
// your first list:
List<Integer> intList = ... ;
// your second list:
List<String> stringList = ... ;
Then define
然后定义
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 ;
}
}
Now you can do
现在你可以做
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)));
}
and define the columns in the usual way.
并以通常的方式定义列。
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:
如果由于某种原因确实无法进行数据转换,则可以将表的类型视为整数,每行的值是两个列表的索引。使用0, 1,...
不超过列表大小的值填充表。这看起来像:
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);
}
}
回答by dbillz
I found a JavaFX tutorial that appears to have solutions for taking collections and populating columns based on them, as well as having the table automatically updated as the collections change later, using an ObservableList.
我找到了一个 JavaFX 教程,该教程似乎提供了获取集合并基于它们填充列的解决方案,以及使用 ObservableList 在集合更改时自动更新表。
http://code.makery.ch/library/javafx-8-tutorial/part2/
http://code.makery.ch/library/javafx-8-tutorial/part2/
Their code has a list of objects with two attributes, you would want to modify their code to draw from your two separate lists instead of accessing the two variables of their objects for the separate columns.
他们的代码有一个具有两个属性的对象列表,您可能希望修改他们的代码以从两个单独的列表中绘制,而不是访问单独列的对象的两个变量。