javafx combobox cell

Solutions on MaxInterview for javafx combobox cell by the best coders in the world

showing results for - "javafx combobox cell"
Claudia
05 Oct 2019
1@FXML AnchorPane root;
2@FXML ComboBox<UserDTO> cmbUsers;
3List<UserDTO> users;
4public class GateInController implements Initializable {
5@Override   
6public void initialize(URL location, ResourceBundle resources) {
7        users = UserService.getListOfUsers();
8        cmbUsers.setItems(FXCollections.observableList(users));
9        cmbUsers.getSelectionModel().selectFirst();
10        // list of values showed in combo box drop down
11        cmbUsers.setCellFactory(new Callback<ListView<UserDTO>,ListCell<UserDTO>>(){
12            @Override
13            public ListCell<UserDTO> call(ListView<UserDTO> l){
14                return new ListCell<UserDTO>(){
15                    @Override
16                    protected void updateItem(UserDTO item, boolean empty) {
17                        super.updateItem(item, empty);
18                        if (item == null || empty) {
19                            setGraphic(null);
20                        } else {
21                            setText(item.getUserId()+"    "+item.getUserNm());
22                        }
23                    }
24                } ;
25            }
26        });
27        //selected value showed in combo box
28        cmbUsers.setConverter(new StringConverter<UserDTO>() {
29              @Override
30              public String toString(UserDTO user) {
31                if (user == null){
32                  return null;
33                } else {
34                  return user.getUserId();
35                }
36              }
37
38            @Override
39            public UserDTO fromString(String userId) {
40                return null;
41            }
42        });
43    }
44}
45