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.

@Component
@FxmlView
public class MapExample {

    private static int i = 0;

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

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

    @FXML
    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");
        col.setCellValueFactory(
                (TableColumn.CellDataFeatures<Map.Entry<String, Person>, String> p) -> new SimpleStringProperty(
                        p.getValue().getKey()));

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

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

        tableview.getColumns().add(col);
        tableview.getColumns().add(col2);
        tableview.getColumns().add(col3);

        tableview.setItems(FXCollections.observableArrayList(map.entrySet()));

        ExecutorService service = Executors.newSingleThreadExecutor();

        service.execute(() -> {

            while (true) {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

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

                System.out.println(map.size());

            }
        });

    }
}

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: https://stackoverflow.com/questions/67384121/javafx-how-to-insert-observablemap-into-tableview

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: