2

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();
        }

    }
}

result:- enter image description here

Please give suitable solution for this problem...

0

3 Answers 3

3

Remove

tableView.getItems().add(csvData);

which adds the entire list of lists to the table view's data list, each time you read a line from the file (so you get n copies of the same list of list of elements in your table's items list),

and add the line

tableView.setItems(csvData);

after the while loop (this tells the table to use the list of lists as its data list).

If you use proper types, instead of raw types (all those warnings your IDE is giving you are there for a reason) then the compiler will catch these errors for you. I.e.:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

import javafx.beans.property.ReadOnlyObjectWrapper;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;

public class FXMLDocumentController {

    @FXML
    private TableView<ObservableList<String>> tableView;
    String headers[] = null;
    String items[] = null;

    // Employee ee;
    List<String> columns = new ArrayList<String>();
    List<String> rows = new ArrayList<String>();
    ObservableList<ObservableList<String>> csvData = FXCollections.observableArrayList();

    @FXML
    private void initialize() {
        Insert();
    }

    public void Insert() {

        File f = new File(
                "C:\\Users\\admin\\Desktop\\Project\\shipforecast\\Data\\Recieve\\ShipId-1432530905282-1.csv");
        if (f.exists() && !f.isDirectory()) {

            try (
                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().add(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.setItems(csvData);

            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            System.out.println("File Not Found");
        }

    }
}
Sign up to request clarification or add additional context in comments.

Comments

1
String[] title = new String[]{"name","address","email"};
        for (String t : title) {
        TableColumn<Model,Object> col = new TableColumn<>(t.toUpperCase());
        col.setCellValueFactory(new PropertyValueFactory<>(t));
        table.getColumns().add(col);
    }



   table.getItems().add(new Model("Haroon","Kapisa","[email protected]"));

Comments

0

I got the answer for this James_D and thank you.

public class FXMLDocumentController implements Initializable {
    
    @FXML private TableView tableView;
     List<String> columns = new ArrayList<String>();
    
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
        try {
            
            CsvReader products = new CsvReader("C:\\Users\\admin\\Desktop\\Project\\shipforecast\\Data\\Recieve\\ShipId-1432530905282-1.csv");
        
            products.readHeaders();
                        
                       String s[]=products.getHeaders();
                       //int count=0;
                       
                       for(int i=0;i<s.length;i++)
                       {
                           columns.add(s[i]);
                       }
                       System.out.println(columns.size());
                       TableColumn [] tableColumns = new TableColumn[columns.size()];     
                      int columnIndex = 0;
                      for(int  k=0 ; k<columns.size(); k++) {
                      final int j = k;
                     TableColumn col = new TableColumn(columns.get(k));
                        
             
    
    
        col.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<ObservableList,String>,ObservableValue<String>>(){                   
           public ObservableValue<String> call(TableColumn.CellDataFeatures<ObservableList, String> param) {                                                                                             
                return new SimpleStringProperty(param.getValue().get(j).toString());                       
            }                   
        });
        tableView.getColumns().addAll(col);
    }
    while (products.readRecord())
            {
                         ObservableList<String> row = FXCollections.observableArrayList();    
                        for(int x=0;x<s.length;x++)
                        {
                            
                            int count=0;
                        //System.out.println(s[x]);
                String Valid = products.get(s[x]);
                                row.addAll(Valid);
                
                
                                
            }
                        
                        tableView.getItems().add(row);
                        }
    
    }   catch (Exception ex) {    
           ex.printStackTrace();
        }    
    
}
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.