JAVAFX editable ComboBox: refresh after changing a value


Question

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.beans.property.SimpleStringProperty;
    import javafx.beans.property.StringProperty;
    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) {
          setName(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")
          );

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

        final ComboBox cb = new ComboBox();
        cb.setItems(persons);
        cb.setEditable(true);
        cb.setConverter(new StringConverter<SimplePerson>() {

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

          @Override
          public SimplePerson fromString(String name) 
          {
            if(cb.getValue() != null)
            {
              ((SimplePerson)cb.getValue()).setName(name);
              cb.show();
              return (SimplePerson)cb.getValue();
            }
            return null;
          }
        });

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

      }

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

    }
1
2
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>() {
        @Override
        public void handle(ActionEvent actionEvent) {
            SimplePerson person = cb.getValue();
            if (null != person) {
                int idx = persons.indexOf(person);
                person.setName(cb.getEditor().getText());
                persons.set(idx, person);
                cb.setValue(person);
            }
        }
    });
2
3/25/2014 8:32:50 AM

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