Populating JavaView TableView with DataFX
I am trying to populate a TableView with data from a csv file with datafx extension.
Unfortunately the tableview is empty and I cannot find any examples anywhere, which indicates my error.
DataSourceReader dsr1 = new FileSource("Example3.csv");
CSVDataSource ds1 = new CSVDataSource(dsr1);
TableView table1 = new TableView();
table1.setItems(ds1.getData());
table1.getColumns().addAll(ds1.getColumns());
System.out.println("#ds1 " + ds1.getData().size()); //returns zero
The code does not throw an exception, which makes it even more cryptic.
although the question is to populate the TableView with the DataFX extension, here is a solution without DataFX:
-
Create a class that represents your tableView's list item, for example:
public class YourItem { private String itemID; private String itemTitle; public String getItemID() { return itemID; } public void setitemID(String itemID) { this.itemID = itemID; } //getter and setter for the other vars, I am lazy. }
-
Parse the csv file and return
List<YourItem>
there are several tutorials and examples for this, you can find it here: http://www.mkyong.com/java/how-to-read-and-parse-csv-file-in-java/ The important thing is that you create
List
objectsYourItem
where each line is an objectYourItem
-
Convert
List<YourItem>
toObservableList<YourItem>
ObservableList<YourItem> obsList = FXCollections.observableArrayList(yourList);
-
finally set the items to the TableView: I won't mention that you need to create columns for the TableView
TableView<YourItem> test = new TableView<YourItem>(); test.setItems(obsList);