Javafx combobox with custom object displays object address though custom cell factory is used

后端 未结 2 1241
遇见更好的自我
遇见更好的自我 2020-12-09 08:52

I have a combobox which shows list of User objects. I have coded a custom cell factory for the combobox:

@FXML ComboBox cmbUserIds;
         


        
2条回答
  •  既然无缘
    2020-12-09 09:58

    Just create and set a CallBack like follows:

    @FXML ComboBox cmbUserIds;
    
    Callback, ListCell> cellFactory = new Callback, ListCell>() {
    
        @Override
        public ListCell call(ListView l) {
            return new ListCell() {
    
                @Override
                protected void updateItem(User item, boolean empty) {
                    super.updateItem(item, empty);
                    if (item == null || empty) {
                        setGraphic(null);
                    } else {
                        setText(item.getId() + "    " + item.getName());
                    }
                }
            } ;
        }
    }
    
    // Just set the button cell here:
    cmbUserIds.setButtonCell(cellFactory.call(null));
    cmbUserIds.setCellFactory(cellFactory);
    

提交回复
热议问题