noshusan
noshusan

Reputation: 313

Javafx TableView not loading data from observableArrayList

I want to load a csv file into my tableView but table is not populating here is my code. i dont know what i am doing wron

public class FXMLDocumentController implements Initializable {
File csv;
@FXML
AnchorPane anchorpane;
Stage stage;
@FXML
TableView dataTable;
@FXML
TextField path;
@FXML
public void browseOnClick(){        
    stage = (Stage) anchorpane.getScene().getWindow();
    FileChooser fileChooser = new FileChooser();
    fileChooser.setTitle("Open CSV File");
    fileChooser.setInitialDirectory(
        new File(System.getProperty("user.home"))
    );
    fileChooser.getExtensionFilters().addAll(
        new FileChooser.ExtensionFilter("Coma separated value", "*.csv")
    );
    csv =  fileChooser.showOpenDialog(stage);
    if(csv != null){
        open();
    }
}
public void open(){
    path.setText(csv.getAbsolutePath());
    loadCSV(csv, dataTable);
}
public void loadCSV(File csv,TableView table)
        {
    try {
        ObservableList<ObservableList> csvData = FXCollections.observableArrayList();    
        Scanner sc = new Scanner(csv);
            while(sc.hasNext()){
                ObservableList<String> row = FXCollections.observableArrayList();
                for(String column: sc.nextLine().split(",")){
                    row.add(column);
                }
                csvData.add(row);
            }
            table.getItems().clear();
            table.getColumns().clear();
            table.setItems(csvData);
            createColums(table, csvData.get(0).size());
        } catch (FileNotFoundException ex) {
            Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
        }
}
public void createColums(TableView table,int size){
    for(int x = 1; x<size; x++){
        TableColumn t = new TableColumn("Paramiter "+x);
        t.setPrefWidth(120);
        table.getColumns().add(t);
    }
    TableColumn t = new TableColumn("Target");
    t.setPrefWidth(120);
    table.getColumns().add(t);

}
@Override
public void initialize(URL url, ResourceBundle rb) {
    path.setEditable (false);
}    
}

but when i run this code the output is an empty table. columns and rows added but there is no data

Upvotes: 0

Views: 846

Answers (1)

brian
brian

Reputation: 10989

You need a cell value factory. The columns need to know how to render the data in the cell. It's best if the table and columns are typed.

public void createColums(TableView table,int size){
    for(int x = 1; x<=size; x++){ //careful with indexes
        TableColumn<ObservableList<String>,String> t = new TableColumn<>("Paramiter "+x);
        t.setPrefWidth(120);
        final int X = x;
        t.setCellValueFactory(p -> new SimpleStringProperty(p.getValue().get(X-1)));
        table.getColumns().add(t);
    }
    TableColumn t = new TableColumn("Target");
    t.setPrefWidth(120);
    table.getColumns().add(t);
}

I would also use TableView<ObservableList<String>> as the type of data in the table.

I didn't test this because you just gave the one file.

Upvotes: 1

Related Questions