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 I get one result.It adding all my csv file column data into one row.
my code:-
public class FXMLDocumentController {`
@FXML
private TableView tableView;
String headers[] = null;
String items[] = null;
Employee ee;
List<String> columns = new ArrayList<String>();
List<String> rows = new ArrayList<String>();
ObservableList<ObservableList> csvData = FXCollections.observableArrayList();
@FXML
private void initialize() {
Insert();
}
public void Insert() {
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);
}
for (int ii = 0; ii < columns.size(); ii++) {
final int finalIdx = ii;
TableColumn<ObservableList<String>, String> column = new TableColumn<>(
columns.get(ii)
);
// column.setText("hghjghjg");
column.setCellValueFactory(param ->
new ReadOnlyObjectWrapper<>(param.getValue().get(finalIdx))
);
/*System.out.println(new ReadOnlyObjectWrapper<>(param.getValue().get(finalIdx)));*/
tableView.getColumns().addAll(column);
}
//tableView.getColumns().addAll(tableColumns);
} else {
ObservableList<String> row = FXCollections.observableArrayList();
row.clear();
items = l.split(",");
for (String item : items) {
System.out.println(item);
row.add(item);
}
csvData.add(row);
}
i++;
tableView.getItems().add(csvData);
}
} else {
System.out.println("File Not Found");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Please give suitable solution for this problem...
