JAVAFX editable ComboBox: refresh after changing a value


I'm still new to JavaFX and need to create a combobox with objects (SimlpePerson) and not strings. I want to edit the shown value in the box itself. Works good for strings but I have problems with SimpleObjects. I made a StringConverter and it also works, I can edit the object shown in the comboBox. But the list itself is not rerendered after that. If I click on the ComboBox I see the original values. How can I change that?

Any suggestion is very welcome!=)

BR and Thank you! Stefan

    import javafx.application.Application;
    import javafx.collections.FXCollections;
    import javafx.collections.ObservableList;
    import javafx.scene.Scene;
    import javafx.scene.control.ComboBox;
    import javafx.scene.control.TableView;
    import javafx.stage.Stage;
    import javafx.util.StringConverter;

    public class ComboBoxDemo extends Application{

      public class SimplePerson {
        private StringProperty name;
        private String somethingElse;

        public SimplePerson(String name) {

        public final void setName(String value) { nameProperty().set(value); }
        public String getName() { return nameProperty().get(); }

        public StringProperty nameProperty() {
          if (name == null) name = new SimpleStringProperty(this, "name");
          return name;

      final ObservableList<SimplePerson> persons = FXCollections.observableArrayList(
            new SimplePerson("Jacob"),
            new SimplePerson("Isabella"),
            new SimplePerson("Ethan"),
            new SimplePerson("Emma"),
            new SimplePerson("Michael")

      public void start(Stage stage) throws Exception {
        // TODO Auto-generated method stub

        final ComboBox cb = new ComboBox();
        cb.setConverter(new StringConverter<SimplePerson>() {

          public String toString(SimplePerson p) 
            if(p != null)
              return p.getName();
            return "";

          public SimplePerson fromString(String name) 
            if(cb.getValue() != null)
              return (SimplePerson)cb.getValue();
            return null;

        stage.setScene(new Scene(cb));;


      public static void main(String[] args) { launch(args); }

3/20/2014 10:08:19 AM

Accepted Answer

Check out this solution. There is a handler which is triggered when you've finished editing. There you may implement all the code which changes the model's state.

To update the combobox list the following approach work may:

 cb.getEditor().setOnAction(new EventHandler<ActionEvent>() {
        public void handle(ActionEvent actionEvent) {
            SimplePerson person = cb.getValue();
            if (null != person) {
                int idx = persons.indexOf(person);
                persons.set(idx, person);
3/25/2014 8:32:50 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow