未观察到组合框

Combo Box is not observed

我有以下代码:

public class GuiView extends Application {

  private ObservableList<String> shareNames = FXCollections.observableArrayList();

  public void start(Stage stage) {
    ...
    ComboBox<String> comboBox = new ComboBox<String>();
    comboBox.getItems().addAll(this.shareNames);

    MenuItem open = new MenuItem("Open...");
    open.setOnAction( e -> {
      // FileChooser code...
      if (selctedFile != null) {
        this.shareNames.addAll("teststring");
      }
    });
  }
}

当我 运行 通过打开的对话框成功时,组合框不会更新并显示测试字符串。这里出了什么问题?

您正在更新 shareNames,但这不是组合框使用的列表。

要么替换

comboBox.getItems().addAll(this.shareNames);

comboBox.setItems(this.shareNames);

或替换

this.shareNames.addAll("teststring");

comboBox.getItems().add("teststring");