Rafael Wörner
Rafael Wörner

Reputation: 319

Combo Box is not observed

I have the following Code:

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

When I run through the open dialog successfully the combo box doesn't update and shows the teststring. What is going wrong here?

Upvotes: 0

Views: 27

Answers (1)

James_D
James_D

Reputation: 209225

You are updating shareNames, but that is not the list used by the combo box.

Either replace

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

with

comboBox.setItems(this.shareNames);

or replace

this.shareNames.addAll("teststring");

with

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

Upvotes: 1

Related Questions