Note that there are some explanatory texts on larger screens.

plurals
  1. POJavafx combobox with custom object displays object address though custom cell factory is used
    primarykey
    data
    text
    <p>I have a combobox which shows list of <code>User</code> objects. I have coded a custom cell factory for the combobox:</p> <pre><code>@FXML ComboBox&lt;User&gt; cmbUserIds; cmbUserIds.setCellFactory(new Callback&lt;ListView&lt;User&gt;,ListCell&lt;User&gt;&gt;(){ @Override public ListCell&lt;User&gt; call(ListView&lt;User&gt; l){ return new ListCell&lt;User&gt;(){ @Override protected void updateItem(Useritem, boolean empty) { super.updateItem(item, empty); if (item == null || empty) { setGraphic(null); } else { setText(item.getId()+" "+item.getName()); } } } ; } }); </code></pre> <p>ListView is showing a string(id+name), but when I select an item from listview, Combobox is showing toString() method return value i.e address of object. I can't override toString() method, because the User domain object should be same as the one at server. How to display id in combobox? Please suggest</p> <p><b>EDIT1</b> </p> <p>I tried below code. Now combo box shows id when I select a value from the listview.</p> <pre><code>cmbUserIds.setConverter(new StringConverter&lt;User&gt;() { @Override public String toString(User user) { if (user== null){ return null; } else { return user.getId(); } } @Override public User fromString(String id) { return null; } }); </code></pre> <p>The selected value in combo box is cleared when control focus is lost. How to fix this?</p> <p><strong>EDIT2:</strong></p> <pre><code>@FXML AnchorPane root; @FXML ComboBox&lt;UserDTO&gt; cmbUsers; List&lt;UserDTO&gt; users; public class GateInController implements Initializable { @Override public void initialize(URL location, ResourceBundle resources) { users = UserService.getListOfUsers(); cmbUsers.setItems(FXCollections.observableList(users)); cmbUsers.getSelectionModel().selectFirst(); // list of values showed in combo box drop down cmbUsers.setCellFactory(new Callback&lt;ListView&lt;UserDTO&gt;,ListCell&lt;UserDTO&gt;&gt;(){ @Override public ListCell&lt;UserDTO&gt; call(ListView&lt;UserDTO&gt; l){ return new ListCell&lt;UserDTO&gt;(){ @Override protected void updateItem(UserDTO item, boolean empty) { super.updateItem(item, empty); if (item == null || empty) { setGraphic(null); } else { setText(item.getUserId()+" "+item.getUserNm()); } } } ; } }); //selected value showed in combo box cmbUsers.setConverter(new StringConverter&lt;UserDTO&gt;() { @Override public String toString(UserDTO user) { if (user == null){ return null; } else { return user.getUserId(); } } @Override public UserDTO fromString(String userId) { return null; } }); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload