当检查JFXListView中的所有JFXCheckBox并且未选中所有JFXCheckBox时,如何触发事件?
这是我的代码:
@FXML
private JFXListView li_se;
private void Services(ActionEvent e) {
li_se.getItems().clear();
for (int i = 0; i < R.OffrirService().size(); i++) {
JFXCheckBox s = new JFXCheckBox(R.OffrirService().get(i));
s.selectedProperty().addListener((obs, wasOn, isNowOn) -> {
if (wasOn == false) {
//if checked do something
} else {
//if unchecked do something
}
//System.out.println(s.getText() + " changed on state from " + wasOn + " to " + isNowOn);
});
li_se.getItems().add(s);
}
}
答案 0 :(得分:1)
@FXML
private JFXListView li_se;
int d;
@FXML
private void Services(ActionEvent e) {
try {
li_se.getItems().clear();
for (int i = 0; i < R.OffrirService().size(); i++) {
JFXCheckBox s = new JFXCheckBox(R.OffrirService().get(i));
s.selectedProperty().addListener((obs, wasOn, isNowOn) -> {
if (wasOn == false) {
//if checked do something
d++;
if (d == R.OffrirService().size()) {
//if all checked do something
}
} else {
//if unchecked do something
d--;
if (d == 0) {
//if all unchecked do something
}
//System.out.println(s.getText() + " changed on state from " + wasOn + " to " + isNowOn);
});
li_se.getItems().add(s);
}
}
答案 1 :(得分:0)
您可以探索Bindings。
选择了2个复选框的示例:
BooleanProperty bp = new SimpleBooleanProperty(false);
CheckBox cb1 = new CheckBox();
CheckBox cb2 = new CheckBox();
bp.bind(Bindings.and(cb1.selectedProperty(), cb2.selectedProperty()));
bp.addListener(new ChangeListener<Boolean>() {
@Override
public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
}
});
对于多个CheckBox,一些奇怪的想法到了。我希望可以解释一下。
像unix chmod 000这样的主要想法 - 无法访问,777 - 所有访问权限。
例如,你有五个CBox。让我们为每个增加一些价值。
CB1值1
CB2值2
CB3值3
CB4值4
CB5值5
并使用someVariable存储所有选定CBox的总值。 未选中时:someVariable == 0。 全部选中时:someVariable ==(1 + 2 + 3 + 4 + 5)
IntegerProperty checkedNumbers = new SimpleIntegerProperty(0);
checkedNumbers.addListener(new javafx.beans.value.ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
if (newValue == 0) {
//doSomething
} else if (newValue == (1 + 2 + 3 + 4 + 5)) {
//doSomethingElse
}
}
});
int cBoxNumbers = 5;
//Implement same ChangeListener for every CBoxes based on different UserData.
//When CB selected, add its value to checkedNumbers
//When unselected - subtract
ChangeListener changeListener;
for (int i = 1; i <= cBoxNumbers; i++) {
CheckBox cb1 = new CheckBox();
cb1.setUserData(i);
cb1.selectedProperty().addListener(changeListener);
}