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