vendredi 12 août 2016

(JavaFX) ChangeListener on CheckBox isn't updating?

I have a two-fold question. The following code is causing me trouble. I have a list of CheckBox objects that I'm trying to set up listeners for:

chk.selectedProperty().addListener(new ChangeListener<Boolean>() {
  @Override
  public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
    System.out.println(newValue);

    if (newValue = false) {
      System.out.println("check " + chk.getText() + " selected!");
    }
  }
});

However, I'm finding that the if (newValue = false) is never executed when selecting and deselecting the CheckBox. I tested changing the test to true and it was still never executed. If I run System.out.println(newValue) immediately before my if statement, it correctly displays either true or false.

This is part of a loop where I'm adding a ChangeListener to each CheckBox in a list. My goal is to be able to determine if the CheckBox has been selected by a mouse click or programmatically selected. If it is selected programmatically, I need to pass the changed CheckBox to a method I've defined elsewhere.

So, two questions: How do I accurately determine whether the CheckBox has been selected or deselected, and how would I pass that particular CheckBox to a method?

Thank you in advance for all your help!




Aucun commentaire:

Enregistrer un commentaire