JavaFx - ComboBox中StringConverter.fromString的用途是什么

时间:2018-01-05 03:27:45

标签: java javafx combobox

在以下片段中,StringConverter.fromString的用途是什么? 我以为转换的Object应该是我选择的那个,而ComboBox应该已经处理过了,那么为什么提供转换后的Object是必须的呢?

public class Person{
      public String firstName;
      public Stirng lastName;
}
@FXML ComboBox<Person> peopleComboBox;
public void setComboBox(){
     // add people to peopleComboBox in here...
     // set object converter
     peopleComboBox.setConverter(new StringConverter<Person>() {
        @Override
        public String toString(Person person) {
            return person.firstName;
        }

        @Override
        public Person fromString(String string) {
            return peopleComboBox.getSelectionModel().getSelectedItem();
        }
    });
}

0 个答案:

没有答案