0

AutoCompleteTextFieldList<T>完全関数としてa を受け入れます。String で初期化することはできますが、タイプ T のオブジェクトで初期化することはできないようAutoCompleteTextField.valuePropertyですSelectionModel

public class GluonApplication extends MobileApplication {

@Override
public void init() {
    ObservableList<Person> persons = getPersons();
    Person defaultPerson = persons.get(0);
    FilteredList<Person> personsFiltered = new FilteredList<>(persons);

    AutoCompleteTextField<Person> txtPerson = new AutoCompleteTextField<>();
    txtPerson.setCompleter(s ->
    {
        personsFiltered.setPredicate(person -> person.name.contains(s));
        return personsFiltered;
    });

    TextField txtSelectedPerson = new TextField();
    txtPerson.valueProperty().addListener((obsValue, p, p1) -> txtSelectedPerson.setText(p1.name));

    Button btnSave = new Button("SAVE");
    btnSave.setOnAction(e ->
    {
        Person selectedPerson = txtPerson.getValue(); //if no autocomplete suggestion was selected, valueProperty is empty
        // save selectedPerson
    });

    VBox root = new VBox(txtPerson, txtSelectedPerson, btnSave);
    root.setAlignment(Pos.CENTER);

    addViewFactory(HOME_VIEW, () ->
    {

        View view = new View(root) {
            @Override
            protected void updateAppBar(AppBar appBar) {
                appBar.setTitleText("AutoCompleteTest");
            }
        };

        view.setOnShowing(e -> txtPerson.setText(defaultPerson.name));  // here I would like to set the person object instead
                                                                        // of its string representation
        return view;
    });
}

private ObservableList<Person> getPersons() {
    return FXCollections.observableArrayList(new Person("person1"), new Person("person2"), new Person("person3"));
}

private static class Person {

    private String name;

    public Person(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return name;
    }

}

}

4

0 に答える 0