I have a ComboBox<Perosn> type Person , in which I added several objects of the Person class,
I used the setCellFactory(Callback) method to display the username in a ComboBox
combobox.setCellFactory( new Callback<ListView<Person >, ListCell<Person >>() { @Override public ListCell<Person > call(ListView<Person > p) { ListCell cell = new ListCell<Person >() { @Override protected void updateItem(Person item, boolean empty) { super.updateItem(item, empty); if (empty) { setText(""); } else { setText(item.getName()); } } }; return cell; } });
And, setButtonCell(ListCell) method to display the name in combobox when selected.
combobox.setButtonCell( new ListCell<Object>() { @Override protected void updateItem(Person t, boolean bln) { super.updateItem(t, bln); if (bln) { setText(""); } else { setText(t.getName()); } } });
This works fine with the normal case, but when I use editable combobox, it fails.
When I write, combobox.setEditable(true); , then when you select an element, the combobox text box (editor) shows the toString() method of the person class.
Normal case: 
Editable Case: 
Is there any solution for this?
I have a model class,
public class Person { String name; int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Person{" + "name=" + name + ", age=" + age + '}'; } }
javafx javafx-2 javafx-8 combobox
Shreyas dave
source share