JavaFX 从 ToggleGroup 取消切换单选按钮

JavaFX untoggle a radio button from a ToggleGroup

我在 ToggleGroup 中有 2 个 RadioButton。我希望能够在单击时取消选择 单选按钮(如果它已被选中)。

SampleController.java(控制器class)

public class SampleController {

    @FXML
    private RadioButton button1;

    @FXML
    private RadioButton button2;

    public void initializeButtons() {
        ToggleGroup toggles = new ToggleGroup();
        button1.setToggleGroup(toggles);
        button2.setToggleGroup(toggles);

        toggles.selectedToggleProperty().addListener(new ChangeListener<Toggle>(){

            public void changed(ObservableValue<? extends Toggle> ov, Toggle old_toggle, Toggle new_toggle) {

                
                System.out.println("curently : " + toggles.getSelectedToggle().isSelected());
             } 
        });
    }

}

如果我保留上面显示的代码,如果我切换一个按钮,那么另一个按钮将取消切换,反之亦然,但我无法取消两个按钮。我希望能够取消选择一个RadioButton,也就是说,移除它的焦点并return它回到原来的状态。

我正在寻找等同于

的东西
buttonGroup.clearSelection()

来自 class ButtonGroup in Swing.


在 Whosebug 上看类似的问题,看来我必须这样做

(...)
public void changed(ObservableValue<? extends Toggle> ov, Toggle old_toggle, Toggle new_toggle) {

                if(toggles.getSelectedToggle() != null) {
                    if(toggles.getSelectedToggle().isSelected()){
                        toggles.getSelectedToggle().setSelected(false);
                    } else {
                        toggles.getSelectedToggle().setSelected(true);
                    }
                }
                System.out.println("curently : " + toggles.getSelectedToggle().isSelected());
             } 
(...)

但是添加这段代码 return 每当我单击任何单选按钮时都会出现以下堆栈跟踪错误

Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
        at SampleController.changed(SampleController.java:32)
        at SampleController.changed(SampleController.java:1)
        at javafx.base/com.sun.javafx.binding.ExpressionHelper$Generic.fireValueChangedEvent(ExpressionHelper.java:360)
        at javafx.base/com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:80)
        at javafx.base/javafx.beans.property.ReadOnlyObjectPropertyBase.fireValueChangedEvent(ReadOnlyObjectPropertyBase.java:74)
        at javafx.base/javafx.beans.property.ReadOnlyObjectWrapper.fireValueChangedEvent(ReadOnlyObjectWrapper.java:102)
        at javafx.base/javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:113)
        at javafx.base/javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:147)
        at javafx.controls/javafx.scene.control.ToggleGroup.set(ToggleGroup.java:138)
        at javafx.controls/javafx.scene.control.ToggleGroup.set(ToggleGroup.java:120)
        at javafx.controls/javafx.scene.control.ToggleGroup.selectToggle(ToggleGroup.java:150)
        at javafx.controls/javafx.scene.control.ToggleButton.invalidated(ToggleButton.java:169)
        at javafx.base/javafx.beans.property.BooleanPropertyBase.markInvalid(BooleanPropertyBase.java:110)
        at javafx.base/javafx.beans.property.BooleanPropertyBase.set(BooleanPropertyBase.java:145)
        at javafx.controls/javafx.scene.control.ToggleButton.setSelected(ToggleButton.java:150)
        at javafx.controls/javafx.scene.control.ToggleButton.fire(ToggleButton.java:256)
        at javafx.controls/javafx.scene.control.RadioButton.fire(RadioButton.java:113)
        at javafx.controls/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:206)
        at javafx.controls/com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274)
        at javafx.base/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
        at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
        at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
        at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
        at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
        at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
        at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
        at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
        at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
        at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
        at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
        at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
        at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
        at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
        at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3851)
        at javafx.graphics/javafx.scene.Scene$MouseHandler.access00(Scene.java:3579)
        at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1849)
        at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2588)
        at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:397)
        at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
        at java.base/java.security.AccessController.doPrivileged(Native Method)
        at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent(GlassViewEventHandler.java:434)
        at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:390)
        at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:433)
        at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:556)
        at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:942)
        at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
        at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication.lambda$runLoop(GtkApplication.java:277)
        at java.base/java.lang.Thread.run(Thread.java:829)

清除切换组中选择的内置方法是

toggles.selectToggle(null);

弄清楚何时调用它来实现你想要的有点棘手。真的,您可能应该使用 ToggleButtons 并重新设置它们的样式,使其看起来像 RadioButtons,或者使用 RadioButtons 和自定义皮肤,将行为重新定义为您想要的.这两个都是相当多的工作(尤其是后者)。

尽管以下快速技巧似乎有效:

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.ButtonBase;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class App extends Application {

    private final ToggleGroup toggles = new ToggleGroup();

    @Override
    public void start(Stage stage) {

        VBox root = new VBox(5);
        root.setPadding(new Insets(20));

        root.getChildren().add(createToggle("Option 1"));
        root.getChildren().add(createToggle("Option 2"));

        Scene scene = new Scene(root);

        stage.setScene(scene);
        stage.show();
    }

    private ButtonBase createToggle(String name) {
        RadioButton toggle = new RadioButton(name);
        toggle.addEventFilter(MouseEvent.MOUSE_PRESSED, e -> {
            if (toggle.isSelected()) {
                toggles.selectToggle(null);
                e.consume();
            }
        });
        toggle.setToggleGroup(toggles);
        return toggle;
    }

    public static void main(String[] args) {
        launch();
    }

}