gpt4 book ai didi

javafx - 如何在Java FXML中向TableView添加动态列和行

转载 作者:行者123 更新时间:2023-12-04 13:22:24 27 4
gpt4 key购买 nike

在java fxml中,我从.csv文件中检索数据。我正在向表 View 添加动态列和行,并且向其中添加了列,但没有添加行。
我正在尝试搜索互联网,但无法获得任何合适的结果。

我的代码:-

public class FXMLDocumentController {

@FXML
private TableView tableView;
String headers[] = null;
String items[] = null;

Employee ee;

@FXML
private void initialize() {
Insert();
}

public void Insert() {
List<String> columns = new ArrayList<String>();
List<String> rows = new ArrayList<String>();
ObservableList<ObservableList> csvData = FXCollections.observableArrayList();

try {
int columnIndex = 0;
TableColumn[] tableColumns;
File f = new File("C:\\Users\\admin\\Desktop\\Project\\shipforecast\\Data\\Recieve\\ShipId-1432530905282-1.csv");
if (f.exists() && !f.isDirectory()) {
FileReader fin = new FileReader(f);
BufferedReader in = new BufferedReader(fin);
String l;
int i = 0;

while ((l = in.readLine()) != null) {

if (i < 1) {
headers = l.split(",");

for (String w : headers) {
columns.add(w);

}

tableColumns = new TableColumn[columns.size()];
columnIndex = 0;
for (String columName : columns) {

//System.out.println("dynamic.FXMLDocumentController.Insert()"+columns.size());
tableColumns[columnIndex++] = new TableColumn(columName);
}

tableView.getColumns().addAll(tableColumns);
} else {
ObservableList<String> row = FXCollections.observableArrayList();
items = l.split(",");
for (String item:items) {

row.add(item);
}
csvData.add(row);
System.out.println("hi");

}
i++;
tableView.getItems().add(csvData);
}
} else {
System.out.println("File Not Found");
}
} catch (Exception e) {
System.out.println(e);
}

}
}

最佳答案

您缺少的是列的cellValueFactory,它将告诉该列在其单元格中显示什么值。

像这样的东西:

TableView<ObservableList<String>> tableView = new TableView<>();
List<String> columnNames = dataGenerator.getNext(N_COLS);
for (int i = 0; i < columnNames.size(); i++) {
final int finalIdx = i;
TableColumn<ObservableList<String>, String> column = new TableColumn<>(
columnNames.get(i)
);
column.setCellValueFactory(param ->
new ReadOnlyObjectWrapper<>(param.getValue().get(finalIdx))
);
tableView.getColumns().add(column);
}

示例应用程序

该解决方案略微基于Narayan的博客文章 Updated: Dynamic TableView Data From Database。该解决方案而不是该博客文章,而是使用测试数据生成器来生成一些虚拟数据和一些Java 8 lambda功能,这些功能使单元格值工厂定义的编写和查看变得不那么麻烦。

fix off by one error
import javafx.application.Application;
import javafx.beans.property.ReadOnlyObjectWrapper;
import javafx.collections.*;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.stage.Stage;

import java.util.*;

public class DynamicTableView extends Application {
private static final int N_COLS = 5;
private static final int N_ROWS = 1_000;

public void start(Stage stage) throws Exception {
TestDataGenerator dataGenerator = new TestDataGenerator();

TableView<ObservableList<String>> tableView = new TableView<>();

// add columns
List<String> columnNames = dataGenerator.getNext(N_COLS);
for (int i = 0; i < columnNames.size(); i++) {
final int finalIdx = i;
TableColumn<ObservableList<String>, String> column = new TableColumn<>(
columnNames.get(i)
);
column.setCellValueFactory(param ->
new ReadOnlyObjectWrapper<>(param.getValue().get(finalIdx))
);
tableView.getColumns().add(column);
}

// add data
for (int i = 0; i < N_ROWS; i++) {
tableView.getItems().add(
FXCollections.observableArrayList(
dataGenerator.getNext(N_COLS)
)
);
}

tableView.setPrefHeight(200);

Scene scene = new Scene(tableView);
stage.setScene(scene);
stage.show();
}

public static void main(String[] args) {
launch(args);
}

private static class TestDataGenerator {
private static final String[] LOREM = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc tempus cursus diam ac blandit. Ut ultrices lacus et mattis laoreet. Morbi vehicula tincidunt eros lobortis varius. Nam quis tortor commodo, vehicula ante vitae, sagittis enim. Vivamus mollis placerat leo non pellentesque. Nam blandit, odio quis facilisis posuere, mauris elit tincidunt ante, ut eleifend augue neque dictum diam. Curabitur sed lacus eget dolor laoreet cursus ut cursus elit. Phasellus quis interdum lorem, eget efficitur enim. Curabitur commodo, est ut scelerisque aliquet, urna velit tincidunt massa, tristique varius mi neque et velit. In condimentum quis nisi et ultricies. Nunc posuere felis a velit dictum suscipit ac non nisl. Pellentesque eleifend, purus vel consequat facilisis, sapien lacus rutrum eros, quis finibus lacus magna eget est. Nullam eros nisl, sodales et luctus at, lobortis at sem.".split(" ");

private int curWord = 0;

List<String> getNext(int nWords) {
List<String> words = new ArrayList<>();

for (int i = 0; i < nWords; i++) {
if (curWord == Integer.MAX_VALUE) {
curWord = 0;
}

words.add(LOREM[curWord % LOREM.length]);
curWord++;
}

return words;
}
}
}

关于javafx - 如何在Java FXML中向TableView添加动态列和行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37559584/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com