JavaFx,如何将 ObservableMap 插入 TableView?

JavaFx, How to insert ObservableMap into TableView?

我想将地图值填充到 table视图中。我尝试使用 observableList 并且它工作正常。每当任何值添加到列表时,table 值都会更新/刷新并显示新的更改。

我也想对地图做同样的事情。每当我向地图 table 添加任何值时,视图都需要刷新,但事实并非如此。看了那么多文章都没能解决这个问题

文章:

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

我的 Java 代码。

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

            }
        });

    }
}

人物模型:

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

我知道有一些小错误,但无法解决。请告诉我哪里错了。

提前致谢。

你可以这样试试。但不要忘记在 FX 线程中填充地图。

@FXML 
private TableView<Map.Entry<? extends String,? extends Person>> table;

....

final ObservableMap<String, Person> map = FXCollections.observableHashMap();
final ObservableList<Map.Entry<? extends String, ? extends Person>> list=FXCollections.observableArrayList();

map.addListener((MapChangeListener<String, Person>) change -> list.setAll(change.getMap().entrySet()));

TableColumn<Map.Entry<? extends String,? extends Person>, String> col = new TableColumn<>("Name");

...        

table.setItems(list);

我犯了一个错误,我将 map.entryset() 添加到 table 中,这是不可观察的。我们需要将 observablelist 添加到 table 视图中,而不是地图。

答案:

@Component
@FxmlView
public class MapExample {

    private static int i = 0;

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

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

    @FXML
    final ObservableList<Map.Entry<? extends String, ? extends Person>> list = FXCollections.observableArrayList();

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

                list.setAll(change.getMap().entrySet());
            } 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<Entry<? extends String, ? extends Person>, String> col = new TableColumn<>("Name");
        col.setCellValueFactory((
                TableColumn.CellDataFeatures<Entry<? extends String, ? extends Person>, String> p) -> new SimpleStringProperty(
                        p.getValue().getKey()));

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

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

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

        tableview.setItems(list);

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

            }
        });

    }
}