1
votes

I am trying to add a value to an editable combo box called nameComboBox that I created in the Scene Builder.

I populate the combo box with this code:

  private ObservableList<String> getNames()
  {
      return (FXCollections.observableArrayList("Freddy","Kerstin"));
  }

..

nameComboBox.getSelectionModel().select(getNames());

I have a Save button defined form the Scene Builder. The code looks like this:

@FXML
  private void handleSaveBtn()
  {
     System.out.println("The new name  is " + nameComboBox.getValue());
  }

When the scene is displayed, the combo boxes editable field is displayed empty with the two names hidden in the list underneath the empty field, which is what I want to happen.

If then type "Rusty" in the empty field and click a save button all that happens is that the println statement returns "The new name is null".

If I wanted to do something with the new value, like validate it or store it in a database, how do I get the value that I entered in the editable field?

2

2 Answers

0
votes

Try using this instead of .getValue() :

nameComboBox.getEditor().getText()

This returns the value of the textProperty of the TextField (.getEditor()) of the editable ComboBox.

0
votes

try this

  nameComboBox.setItems(getNames());
   nameComboBox.setValue("Freddy");