3

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.

Tim Büthe
  • 62,884
  • 17
  • 92
  • 129
  • CheckComboBox has `javafx.scene.control.ComboBox` internally, which in turn has properties onHiding, onShowing etc. Try to reach, if possible, to this combobox roughly by checkComboBox.getSkin().getChildren(0). See the internal combobox [here](https://bitbucket.org/controlsfx/controlsfx/src/b0256e97a7fefc8d470f9eec5bfb87c5d376b05b/controlsfx/src/main/java/impl/org/controlsfx/skin/CheckComboBoxSkin.java?at=default). – Uluk Biy Aug 07 '14 at 13:21
  • `checkComboBox.getSkin()` returns `null` (version 8.0.6) – Tim Büthe Aug 07 '14 at 13:41

2 Answers2

2

I used

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

Seemed pretty clean.

Brad Turek
  • 2,472
  • 3
  • 30
  • 56
1

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)