Tim Büthe
Tim Büthe

Reputation: 63754

How to listen to open/close events of a CheckComboBox?

I'm using ControlsFX's CheckComboBox and want to listen for open and close events of the menu. Is there a way to do that?

I need this, to commit the done changes when the users closes the menu / leaves the field. In TextFields I do this when the user hits Enter, which doesn't seem appropriate using this control. Alternatively, I could try working with focusedProperty in some way.

Upvotes: 3

Views: 2816

Answers (2)

Brad Turek
Brad Turek

Reputation: 2852

I used

// Commit only when box closes
checkComboBox.addEventHandler(ComboBox.ON_HIDDEN, event -> {
    System.out.println("CheckComboBox is now hidden.");
});

Seemed pretty clean.

Upvotes: 2

AnotherNewbplusplus
AnotherNewbplusplus

Reputation: 215

Old question but may help someone. Original source came from: https://bitbucket.org/controlsfx/controlsfx/issues/462/checkcombobox-ignores-prefwidth-maybe-any by Olivier Vanrumbeke

To reach the combobox from CheckComboBox, try this if the skin is not null:

 CheckComboBoxSkin skin = (CheckComboBoxSkin)checkComboBox.getSkin();
 ComboBox combo = (ComboBox)skin.getChildren().get(0);
 combo.showingProperty().addListener((obs, hidden, showing) -> {
     if(hidden) performTaskWhenPopUpCloses();});

And if it's not set yet (skin is null), try this (ugly workaround):

private final ChangeListener<Skin> skinListener = (skinObs, oldVal, newVal) -> {

    if (oldVal == null && newVal != null) {

        CheckComboBoxSkin skin = (CheckComboBoxSkin) newVal;
        ComboBox combo = (ComboBox) skin.getChildren().get(0);
        combo.showingProperty().addListener((obs, hidden, showing) -> {
            if(hidden)
                performTaskWhenPopUpCloses();

        });
    }
};

checkComboBox.skinProperty().addListener(skinListener);

(version 8.40.9)

Upvotes: 1

Related Questions