Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

javafx - How to add dynamic columns and rows to TableView in java fxml

In java fxml I am retrieving the data from .csv file. 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 but I could not get any suitable result.

My code:-

public class FXMLDocumentController {

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

    Employee ee;

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

    public void Insert() {
        List<String> columns = new ArrayList<String>();
        List<String> rows = new ArrayList<String>();
        ObservableList<ObservableList> csvData = FXCollections.observableArrayList();

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

                        }

                        tableColumns = new TableColumn[columns.size()];
                        columnIndex = 0;
                        for (String columName : columns) {

                            //System.out.println("dynamic.FXMLDocumentController.Insert()"+columns.size());
                            tableColumns[columnIndex++] = new TableColumn(columName);
                        }

                        tableView.getColumns().addAll(tableColumns);
                    } else {
                        ObservableList<String> row = FXCollections.observableArrayList();
                        items = l.split(",");
                        for (String item:items) {

                            row.add(item);
                        }
                        csvData.add(row);
                        System.out.println("hi");

                    }
                    i++;
                    tableView.getItems().add(csvData);
                }
            } else {
                System.out.println("File Not Found");
            }
        } catch (Exception e) {
            System.out.println(e);
        }

    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

What you are missing is a cellValueFactory for your columns that will tell the column what value to display in its cells.

Something like this:

TableView<ObservableList<String>> tableView = new TableView<>();
List<String> columnNames = dataGenerator.getNext(N_COLS);
for (int i = 0; i < columnNames.size(); i++) {
    final int finalIdx = i;
    TableColumn<ObservableList<String>, String> column = new TableColumn<>(
            columnNames.get(i)
    );
    column.setCellValueFactory(param ->
            new ReadOnlyObjectWrapper<>(param.getValue().get(finalIdx))
    );
    tableView.getColumns().add(column);
}

Sample Application

This solution was based slightly on Narayan's blog post: Updated: Dynamic TableView Data From Database. Rather than that blog post, this solution uses a test data generator to generate some dummy data and some of the Java 8 lambda features which make the cell value factory definition a bit less unwieldy to write and look at.

fix off by one error

import javafx.application.Application;
import javafx.beans.property.ReadOnlyObjectWrapper;
import javafx.collections.*;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.stage.Stage;

import java.util.*;

public class DynamicTableView extends Application {    
    private static final int N_COLS = 5;
    private static final int N_ROWS = 1_000;

    public void start(Stage stage) throws Exception {
        TestDataGenerator dataGenerator = new TestDataGenerator();

        TableView<ObservableList<String>> tableView = new TableView<>();

        // add columns
        List<String> columnNames = dataGenerator.getNext(N_COLS);
        for (int i = 0; i < columnNames.size(); i++) {
            final int finalIdx = i;
            TableColumn<ObservableList<String>, String> column = new TableColumn<>(
                    columnNames.get(i)
            );
            column.setCellValueFactory(param ->
                    new ReadOnlyObjectWrapper<>(param.getValue().get(finalIdx))
            );
            tableView.getColumns().add(column);
        }

        // add data
        for (int i = 0; i < N_ROWS; i++) {
            tableView.getItems().add(
                    FXCollections.observableArrayList(
                            dataGenerator.getNext(N_COLS)
                    )
            );
        }

        tableView.setPrefHeight(200);

        Scene scene = new Scene(tableView);
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }

    private static class TestDataGenerator {
        private static final String[] LOREM = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc tempus cursus diam ac blandit. Ut ultrices lacus et mattis laoreet. Morbi vehicula tincidunt eros lobortis varius. Nam quis tortor commodo, vehicula ante vitae, sagittis enim. Vivamus mollis placerat leo non pellentesque. Nam blandit, odio quis facilisis posuere, mauris elit tincidunt ante, ut eleifend augue neque dictum diam. Curabitur sed lacus eget dolor laoreet cursus ut cursus elit. Phasellus quis interdum lorem, eget efficitur enim. Curabitur commodo, est ut scelerisque aliquet, urna velit tincidunt massa, tristique varius mi neque et velit. In condimentum quis nisi et ultricies. Nunc posuere felis a velit dictum suscipit ac non nisl. Pellentesque eleifend, purus vel consequat facilisis, sapien lacus rutrum eros, quis finibus lacus magna eget est. Nullam eros nisl, sodales et luctus at, lobortis at sem.".split(" ");

        private int curWord = 0;

        List<String> getNext(int nWords) {
            List<String> words = new ArrayList<>();

            for (int i = 0; i < nWords; i++) {
                if (curWord == Integer.MAX_VALUE) {
                    curWord = 0;
                }

                words.add(LOREM[curWord % LOREM.length]);
                curWord++;
            }

            return words;
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...