import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.property.SimpleStringProperty;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

public class TableViewSample extends Application {
    private TableView<Person> table = new TableView<Person>();
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) {
        Group root = new Group();
        Scene scene = new Scene(root);
        stage.setWidth(450);
        stage.setHeight(500);
        table.setEditable(true);

        TableColumn firstNameCol = new TableColumn("First Name");
        firstNameCol.setMinWidth(100);
        firstNameCol.setCellValueFactory(
                new PropertyValueFactory<Person, String>("firstName"));

        table.getColumns().addAll(firstNameCol);

        final VBox vbox = new VBox();
        vbox.setSpacing(5);
        vbox.setPadding(new Insets(10, 0, 0, 10));
        vbox.getChildren().addAll(table);

        stage.setOnShown(e -> {
            Thread thread = new Thread() {
                public void run() {
                    for (int i = 0; i < 500; ++i) {
                        CountDownLatch latch = new CountDownLatch(1);
                        final int c = i;
                        Platform.runLater(new Runnable() {
                            @Override
                            public void run() {
                                table.getItems().add(new Person(c + "_Name"));
                                table.getColumns().get(0).setVisible(false);
                                table.getColumns().get(0).setVisible(true);
                                latch.countDown();
                            }
                        });
                        waitForLatch(latch, 200, "ERRORRRRRRR");
                        try {
                            Thread.sleep(50);
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }
                    }
                }
            };
            thread.start();
        });

        root.getChildren().addAll(vbox);
        stage.setScene(scene);
        stage.show();
    }

    public static void waitForLatch(CountDownLatch latch, int seconds, String msg) {
        try {
            if (!latch.await(seconds, TimeUnit.SECONDS)) {
                System.out.println("Errorrrrrrr");
            }
        } catch (Exception ex) {
            System.out.println("Unexpected exception: " + ex);
        }
    }

    public static class Person {
        private final SimpleStringProperty firstName;
        private Person(String fName) {
            this.firstName = new SimpleStringProperty(fName);
        }

        public String getFirstName() {
            return firstName.get();
        }
        public void setFirstName(String fName) {
            firstName.set(fName);
        }
    }
}