i got a problem with filling a tableview with values. I tried the solution of JavaFX TableView dynamic column and data values but this didnt work for me. Here is the code. If anybody knows how to solve this problem this would be really nice.
public class CSVTableViewer extends Application {
private String allColumns;
private FileSource fileSource;
BufferedReader in;
private String zeile;
private String reihe;
private String[]rowsArray;
private String a="";
private String result2;
ArrayList<String> c=new ArrayList<String>();
List<List<String>> data;
/**
* @return the zeile
*/
public String getZeile() {
return zeile;
}
public void start(Stage stage) throws IOException {
TableView tableView = new TableView();
Pattern p = Pattern.compile( "[;\\/|\\t]" );
Pattern p2 = Pattern.compile("[\\s\\s]");
//DataSourceReader dsr1 = new FileSource("your csv file path");
String input = Dialogs.showInputDialog(stage, "Please enter path to csv:", "Input
Dialog", "Input Path");
try{
this.fileSource = new FileSource(input);
in = new BufferedReader(new FileReader(input));
allColumns=in.readLine();
}catch(IOException e){
Dialogs.showErrorDialog(stage, "No CSV-File Found");
System.exit(0);
}
// Pattern p = Pattern.compile( "[;\\.,/|.\\t]" );
String[] columnsArray=p.split(allColumns);// create array of column names you want to
display
for(int i=0;i<columnsArray.length;i++){
// a.add(columnsArray[i]);
}
CSVDataSource ds1 = new CSVDataSource(this.fileSource,columnsArray);
//TableView tableView = new TableView();
// tableView.setItems(ds1.getData());
tableView.getColumns().addAll(ds1.getColumns());
tableView.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
try {
while ((reihe = in.readLine()) != null) {
String[]row=p.split(reihe);
// System.out.println(als);
for (int i = 0; i < row.length; i++) {
String result=row[i].toString();
c.add(result);
}}
}catch (IOException ex) {
Logger.getLogger(CSVTableViewer.class.getName()).log(Level.SEVERE, null, ex);
}
/*
ObservableList<ObservableList> csvData = FXCollections.observableArrayList();
for(List<String> dataList : data) {
ObservableList<String> row = FXCollections.observableArrayList();
for( String rowData : dataList) {
row.add(rowData);
}
/ add each row to cvsData
}
tableView.setItems(csvData);
*/
/* Fills 'columns' and 'data' */
// zeile=in.readLine();
// System.out.println(zeile);
// rowsArray=p.split(zeile);
//TableView tableView = new TableView();
// tableView.setItems(musics);
//ObservableList musics = FXCollections.observableArrayList();
//musics.add(a);
//CSVDataSource ds2 = new CSVDataSource(fileSource,rowsArray);
//TableView tableView = new TableView();
//tableView.setItems(ds2.g);
// } catch (IOException ex) {
// Logger.getLogger(CSVTableViewer.class.getName()).log(Level.SEVERE, null, ex);
// }
//tableView.getColumns().addAll(ds2.getData());
//CSVDataSource ds2 = new CSVDataSource(fileSource,rowsArray);
//TableView tableView = new TableView();
// tableView.setItems(musics);
//tableView.getColumns().addAll(ds1.getColumns());
// ObservableList musics = FXCollections.observableArrayList();
//musics.add(a);
// tableView.setItems(musics);
ObservableList<ObservableList> csvData = FXCollections.observableArrayList();
ObservableList<String> row = FXCollections.observableArrayList();
for( String rowData : c) {
row.add(rowData);
// add each row to cvsData
}
csvData.add(row);
tableView.setItems(csvData);
/*
for(List<String> dataList : data) {
ObservableList<String> row = FXCollections.observableArrayList();
ObservableList<String> row = FXCollections.observableArrayList();
for( String rowData : c) {
row.add(rowData);
}
tableView.setItems(row);
stage.setScene(SceneBuilder.create().root(tableView).build());
stage.show();
// stage.setScene(SceneBuilder.create().root(tableView).build());
// stage.show();
/*
stage.setTitle("Test App");
// Just loading the file...
FileSource fs = new FileSource("artikel_demo.csv");
// Now creating my datasource
CSVDataSource dataSource = new CSVDataSource(
fs, "order-id", "order-item-id");
@SuppressWarnings("rawtypes")
TableView table1 = new TableView();
TableColumn<?, ?> orderCol = dataSource.getNamedColumn("order-id");
TableColumn<?, ?> itemCol = dataSource.getNamedColumn("order-item-id");
table1.getColumns().addAll(orderCol, itemCol);
table1.setItems(dataSource.getData());
stage.setScene(SceneBuilder.create().root(table1).build());
stage.show();
*/
//stage.setScene(SceneBuilder.create().root(tableView).build());
//stage.show();
// scene.getRoot().getChildren();
stage.setScene(SceneBuilder.create().root(tableView).build());
stage.show();
}
/**
* The main() method is ignored in correctly deployed JavaFX application.
* main() serves only as fallback in case the application can not be
* launched through deployment artifacts, e.g., in IDEs with limited FX
* support. NetBeans ignores main().
*
* @param args the command line arguments
*/
public static void main(String[] args) {
Application.launch(args);
}
}