在以下片段中,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();
}
});
}