In java fxml I am retrieving the data from .csv file. I am adding dynamic columns and rows to table view and columns are adding to it but not the rows. I am trying and searching internet but I could not get any suitable result.
My code:-
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);
}
}
}