这个问题有点相反。我有一组ToggleButtons,它们看起来像RadioButtons,同时保持了一次取消选择所有按钮的能力。我该怎么做?在这种情况下,该问题的已接受答案的“对立”不起作用;它只会删除按钮的所有样式,仅保留其标签。
//this doesn't work
ToggleButton button=new ToggleButton("Toggle me!");
button.getStyleClass().remove("toggle-button");
button.getStyleClass().add("radio-button");
您不需要这种样式操作。RadioButton
类extends ToggleButton
,因此您可以执行以下操作:
ToggleButton button = new RadioButton("Toggle me!");
为了保持ToggleButton
行为ToggleGroup
(能够取消选择),您可以将RadioButton
带有重写fire()
方法的实现与ToggleButton
类中的逻辑一起使用:
public static class MyRadioButton extends RadioButton {
public MyRadioButton() {
}
public MyRadioButton(String text) {
super(text);
}
@Override
public void fire() {
if (!isDisabled()) {
setSelected(!isSelected());
fireEvent(new ActionEvent());
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句