JavaFX 三态复选框
JavaFX Three-State Checkbox
给定:
我有一个 javafx.scene.control.CheckBox
,我想允许 3 种状态而不是只有 2 种(selected
和 not selected
)。
我阅读了 indeterminate
状态。
根据 JavaDocs http://docs.oracle.com/javafx/2/api/javafx/scene/control/CheckBox.html#allowIndeterminateProperty
checked: indeterminate == false, checked == true
unchecked: indeterminate == false, checked == false
undefined: indeterminate == true
问题:
我只需要允许从 indeterminate
到 selected
一次
CheckBox checkbox = new CheckBox("Indeterminate");
checkbox.setAllowIndeterminate(true);
checkbox.setIndeterminate(true);
checkbox.setSelected(false);
checkbox.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent e) {
if (checkbox.isIndeterminate()) {
checkbox.setSelected(true);
checkbox.setIndeterminate(false);
checkbox.setAllowIndeterminate(false);
checkbox.setText("Checked");
} else if (checkbox.isSelected()) {
checkbox.setSelected(false);
checkbox.setText("Unchecked");
} else if (!checkbox.isSelected()) {
checkbox.setSelected(true);
checkbox.setText("Checked");
}
}
});
有趣的是,当我第一次点击复选框时,它直接进入 else if (checkbox.isSelected())
块!!!
问题:
什么???!如何?为什么这不起作用?
我需要的是,如果它最初是不确定的,那么在第一次点击时,它应该被选中。并继续点击更多:选中 -> 未选中,未选中 -> 选中,...
否则,如果它最初不是不确定的(即选中或未选中),那么它应该表现正常:选中 -> 未选中,未选中 -> 选中,...
当你设置allowIndeterminateProperty为真时,你实际上做的是让CheckBox
循环所有三种状态:
Determines whether the user toggling the CheckBox should cycle through
all three states: checked, unchecked, and undefined. If true then all
three states will be cycled through; if false then only checked and
unchecked will be cycled.
在你的情况下,它应该设置为 false,因为你希望它只是在 checked 和 unchecked.[=20 之间循环=]
然后如果你设置 indeterminateProperty initially true 或 false CheckBox
将只循环 checked 和未选中 状态。
你的CheckBox
进入isSelected分支的原因是引用中定义的循环:checked -> unchecked -> undefined ->检查 -> ...。由于 checkbox.setIndeterminate(true);
,您的 CheckBox
处于未定义状态,正在检查下一个状态。
例子
这个 CheckBox
最初是 undefined 然后它循环 checked 和 unchecked .
CheckBox checkBox = new CheckBox();
checkBox.indeterminateProperty().set(true);
checkBox.setAllowIndeterminate(false);
checkBox.selectedProperty().addListener((obs, oldVal, newVal) -> {
if(newVal)
System.out.println("Checked");
else
System.out.println("Unchecked");
});
给定:
我有一个 javafx.scene.control.CheckBox
,我想允许 3 种状态而不是只有 2 种(selected
和 not selected
)。
我阅读了 indeterminate
状态。
根据 JavaDocs http://docs.oracle.com/javafx/2/api/javafx/scene/control/CheckBox.html#allowIndeterminateProperty
checked: indeterminate == false, checked == true
unchecked: indeterminate == false, checked == false
undefined: indeterminate == true
问题:
我只需要允许从 indeterminate
到 selected
一次
CheckBox checkbox = new CheckBox("Indeterminate");
checkbox.setAllowIndeterminate(true);
checkbox.setIndeterminate(true);
checkbox.setSelected(false);
checkbox.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent e) {
if (checkbox.isIndeterminate()) {
checkbox.setSelected(true);
checkbox.setIndeterminate(false);
checkbox.setAllowIndeterminate(false);
checkbox.setText("Checked");
} else if (checkbox.isSelected()) {
checkbox.setSelected(false);
checkbox.setText("Unchecked");
} else if (!checkbox.isSelected()) {
checkbox.setSelected(true);
checkbox.setText("Checked");
}
}
});
有趣的是,当我第一次点击复选框时,它直接进入 else if (checkbox.isSelected())
块!!!
问题:
什么???!如何?为什么这不起作用?
我需要的是,如果它最初是不确定的,那么在第一次点击时,它应该被选中。并继续点击更多:选中 -> 未选中,未选中 -> 选中,...
否则,如果它最初不是不确定的(即选中或未选中),那么它应该表现正常:选中 -> 未选中,未选中 -> 选中,...
当你设置allowIndeterminateProperty为真时,你实际上做的是让CheckBox
循环所有三种状态:
Determines whether the user toggling the CheckBox should cycle through all three states: checked, unchecked, and undefined. If true then all three states will be cycled through; if false then only checked and unchecked will be cycled.
在你的情况下,它应该设置为 false,因为你希望它只是在 checked 和 unchecked.[=20 之间循环=]
然后如果你设置 indeterminateProperty initially true 或 false CheckBox
将只循环 checked 和未选中 状态。
你的CheckBox
进入isSelected分支的原因是引用中定义的循环:checked -> unchecked -> undefined ->检查 -> ...。由于 checkbox.setIndeterminate(true);
,您的 CheckBox
处于未定义状态,正在检查下一个状态。
例子
这个 CheckBox
最初是 undefined 然后它循环 checked 和 unchecked .
CheckBox checkBox = new CheckBox();
checkBox.indeterminateProperty().set(true);
checkBox.setAllowIndeterminate(false);
checkBox.selectedProperty().addListener((obs, oldVal, newVal) -> {
if(newVal)
System.out.println("Checked");
else
System.out.println("Unchecked");
});