samedi 7 décembre 2019

Javafx combobox with checkbox

I want to know how to change the selectionmodel of javafxml combobox so that it can allow multiple seletion. I try to create a JavaFX ComboBox with CheckBoxes in the dropdown menu. My code:

public void alLStatus() throws SQLException {

    allStatus.clear();
    cb_statuItem.getItems().clear();
    DbManager test = new DbManager();
    String sql = "SELECT allStatus FROM Items ;";

    Statement stmt = test.connect().createStatement();
    ResultSet rs = stmt.executeQuery(sql);

    while (rs.next()) {
        Items a = new Items();
        a.setStatusItems(rs.getString("statusItems"));
        allStatus.add(a);
    }
    test.disconnect();
    cb_statuItem.setItems(allStatus);

    cb_statuItem.setConverter(new StringConverter<Items>() {
        @Override
        public String toString(Items object) {

            // //System.out.print("converting object: ");
            if (object == null) {
                // //System.out.println("null");
                return "[none]";
            }
            // //System.out.println(object.toString());
            return object.getStatusItems();
        }

        @Override
        public Items fromString(String string) {
            throw new RuntimeException("not required for non editable ComboBox");
        }
    });


    cb_statuItem.setCellFactory(new Callback<ListView<Items>, ListCell<Items>>() {
            @Override
            public ListCell<Items> call(ListView<Items> param) {
                return new ListCell<Items>() {
                    private CheckBox cb = new CheckBox();
                    private BooleanProperty booleanProperty;

                    {
                        cb.setOnAction(e->getListView().getSelectionModel().select(getItem()));
                    }
                    @Override
                    protected void updateItem(Items item, boolean empty) {
                        super.updateItem(item, empty);
                        if (!empty) {


                                booleanProperty = item.selectedProperty();
                                cb.selectedProperty().bindBidirectional(booleanProperty); 
                                setGraphic(cb);
                                setText(item.getStatusItems() + "");

                        } else {
                            setGraphic(null);
                            setText(null);
                        }
                    }
                };
            }
        });



}

ComboBox show text like status of Items but don't allow to click on checkbox, it's automatically close. I want to check more items from comboBox and get them in list. BooleanProperty is always return false.

Any contribution will be appreciated thanks.




Aucun commentaire:

Enregistrer un commentaire