There is a source csv data file. Depending on the content of the file you need to create a table. The number of rows in the table is equal to the number of rows in the file, the number of columns is respectively equal to the number of values ​​in one row. Here is the actual code:

Class inputDate.java

public class InputDate { private ArrayList<String> items = new ArrayList<>(); public InputDate(String[] items){ this.items.addAll(Arrays.asList(items)); } public ArrayList<String> getItems(){ return items; } } 

JavaFXKursovaya.java class

 public class JavaFXKursovaya extends Application { private TabPane root = new TabPane(); private ObservableList<InputDate> os1 = FXCollections.observableArrayList(); private int rowsInpDate = 0; private int colsInpDate = 0; @Override public void start(Stage primaryStage) { //Create view Tab tab1 = new Tab("Загрузка БД"); HBox navDB = new HBox(20.0); Button downloadDB = new Button("Загрузить БД"); ComboBox listOfTables = new ComboBox(); FlowPane content_tab1 = new FlowPane(Orientation.VERTICAL); TableView<InputDate> tableDB = new TableView(); downloadDB.setOnAction((ae)-> { createContent_tab1(tableDB); }); navDB.getChildren().addAll(downloadDB, listOfTables); content_tab1.getChildren().addAll(navDB, tableDB); tab1.setContent(content_tab1); root.getTabs().add(tab1); //settings for root root.setTabClosingPolicy(TabClosingPolicy.UNAVAILABLE); //Creating the scene primaryStage.setTitle("Система обработки входных данных"); primaryStage.setScene(new Scene(root, 800, 600)); primaryStage.show(); } //First tab controller public void createContent_tab1(TableView<InputDate> tableDB){ tableDB.getColumns().clear(); tableDB.getItems().clear(); csvParser(); //Fill the table for(int i = 0; i <= colsInpDate - 1; i++){ TableColumn<InputDate, String> tableColumn = new TableColumn<>("" + i); tableColumn.setCellValueFactory(new PropertyValueFactory<>("items[" + i + "]")); tableDB.getColumns().add(tableColumn); } for (int i = 0; i < os1.size(); i++) System.out.println("os1 index " + i + " = " + os1.get(i).getItems()); tableDB.setItems(os1); tableDB.setEditable(false); tableDB.setFixedCellSize(25.0); } //Parser for csv file public void csvParser(){ String csvFile = "C://Users/Wixes/Desktop/Development/Software" + "/JavaFXKursovaya/src/javafxkursovaya/csvFiles/text.csv"; String line = ""; String csvSplitBy = ","; try (BufferedReader br = new BufferedReader(new FileReader(csvFile))){ rowsInpDate = 0; colsInpDate = 0; while ((line = br.readLine()) != null){ rowsInpDate += 1; System.out.println("rows = " + rowsInpDate); String[] fields = line.split(csvSplitBy, -1); colsInpDate = fields.length; System.out.println("cols = " + colsInpDate); InputDate inpd = new InputDate(fields); System.out.println("input date model = " + inpd.getItems()); os1.add(inpd); } } catch (IOException e){ e.printStackTrace(); } } /** * @param args the command line arguments */ public static void main(String[] args) { launch(args); } } 

text.csv

 0,1,2,3,4,5 6,7,8,9,10,11 12,13,14,15,16,17 

Measurement Control System.out.println displays:

 rows = 1 cols = 6 input date model = [0, 1, 2, 3, 4, 5] rows = 2 cols = 6 input date model = [6, 7, 8, 9, 10, 11] rows = 3 cols = 6 input date model = [12, 13, 14, 15, 16, 17] os1 index 0 = [0, 1, 2, 3, 4, 5] os1 index 1 = [6, 7, 8, 9, 10, 11] os1 index 2 = [12, 13, 14, 15, 16, 17] 

When you click on the button, a table is created with the correct number of columns and rows (when you click on the rows, they are highlighted, if there are 3 rows in the csv, then in table 3, if 5, then in table 5, etc.). But the data in the table is not displayed, all cells are empty. There is a suspicion that incorrectly created the InputDate class, but attempts to modify it did not lead to any result. What have I done wrong?

    1 answer 1

    Replace unit:

     //Fill the table for(int i = 0; i <= colsInpDate - 1; i++){ TableColumn<InputDate, String> tableColumn = new TableColumn<>("" + i); tableColumn.setCellValueFactory(new PropertyValueFactory<>("items[" + i + "]")); tableDB.getColumns().add(tableColumn); } 

    on

     //Fill the table for(int i = 0; i <= colsInpDate - 1; i++){ final int indexColumn = i; TableColumn<InputDate, String> tableColumn = new TableColumn<>("" + i); tableColumn.setCellValueFactory(param -> new SimpleStringProperty(param.getValue().getItems().get(indexColumn))); tableDB.getColumns().add(tableColumn); } 

    Using PropertyValueFactory in this case is not appropriate, because You do not have a specific data model with named fields.

    • Thank you very much, everything works! - Dmitry Komar