JavaFx, How to insert ObservableMap into TableView?

I want to populate map values into tableview. I tried with observableList and its working fine. Whenever any value adds to list then table values are updates / refresh and shows new changes.

I want to do the same with map also. Whenever I adds any value to map table view needs to be refresh but it is not. I read so many articles but not able to solve this issue.

Article :

  1. Populate TableView with ObservableMap JavaFX
  2. How to display an ObservableMap via JavaFX's TableView

My Java code.

public class MapExample {

    private static int i = 0;

    private TableView<Map.Entry<String, Person>> tableview;

    private ObservableMap<String, Person> map = FXCollections.observableHashMap();

    public void initialize() {

        System.out.println("Map Example !!");

        map.put("Vijay", new Person("Rahul", "Kakade"));

        map.addListener((MapChangeListener<String, Person>) change -> {
            if (change.wasAdded()) {
                System.out.println("If part !!");
                map.forEach((k, v) -> System.out
                        .println("Key :" + k + " Name :" + v.getFirstName() + " Last :" + v.getLastName()));
            } else if (change.wasRemoved()) {
                System.out.println("Else part !!");
                map.forEach((k, v) -> System.out
                        .println("Key :" + k + " Name :" + v.getFirstName() + " Last :" + v.getLastName()));

        TableColumn<Map.Entry<String, Person>, String> col = new TableColumn<>("Name");
                (TableColumn.CellDataFeatures<Map.Entry<String, Person>, String> p) -> new SimpleStringProperty(

        TableColumn<Map.Entry<String, Person>, String> col2 = new TableColumn<>("FirstName");
                (TableColumn.CellDataFeatures<Map.Entry<String, Person>, String> p) -> new SimpleStringProperty(

        TableColumn<Map.Entry<String, Person>, String> col3 = new TableColumn<>("LastName");
                (TableColumn.CellDataFeatures<Map.Entry<String, Person>, String> p) -> new SimpleStringProperty(



        ExecutorService service = Executors.newSingleThreadExecutor();

        service.execute(() -> {

            while (true) {
                try {
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block

                map.put("Vijay", new Person("Rahul" + i++, "Kakade" + i));




Person Model :

public class Person {
    private String firstName = null;
    private String lastName = null;
    //getter and setter

I know there is some small mistake but not able to solve it. Please tell me where I am wrong.

Thanks in advance.

Read more here:

Content Attribution

This content was originally published by vijayk at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: