1

Can't find a proper way to find the existing answers, so here is the sketch example describing my situation:

public class MyRButton {
    RadioButton rb;
    MyRButton (RadioButton _rb) {
         rb = new RadioButton(_rb);
         rb.setOnAction(this::handleSelectedAction);
    }

    handleSelectedAction(ActionEvent _selected) {
        // DO if RadioButton rb is selected directly (by mouse etc.)
        // Some external actions are able to reset isSelected() state of the 
        // RadioButton during action handling, so to make sure it's still
        // selected after method processing:
        rb.setSelected(true);   // HERE IS THE DOUBT IF THIS OPERATOR CALLS
        // handleSelectedAction(ActionEvent _selected) RECURSIVELY     
    }
}

Do I have to surround rb.setSelected(true) with disable/enable action handler instruction?

    handleSelectedAction(ActionEvent _selected) {
        // DO if RadioButton rb is selected directly (by mouse etc.)
        rb.setOnAction(null);
        rb.setSelected(true);
        rb.setOnAction(this::handleSelectedAction);
    }

Original code works well, but I suspect if the handleSelectedAction method is running on the background permanently.

Yogene
  • 31
  • 4
  • 1
    Please provide a [mcve] that demonstrates the problem. – kleopatra Aug 27 '18 at 12:21
  • Hi, welcome to StackOverflow! I would resume your doubts as: 1) does RadioButton's setSelected fire setOnAction event?. 2) Should I call setSelected to ensure that's selected? 3) If the two above are true, do I have to disable/enable event to be sure? – Rafael Palomino Aug 27 '18 at 12:51
  • I can answer only to 2nd point I mentioned. To ensure that's selected, you should put an `if (!rb.isSelected())` and then call `rb.setSelected()` – Rafael Palomino Aug 27 '18 at 12:54

1 Answers1

2

OK, guys, according to kleopatra's request I've composed a brief sample which can be run:

    package rbexample;

    import javafx.application.Application;
    import javafx.event.ActionEvent;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.control.RadioButton;
    import javafx.scene.layout.VBox;
    import javafx.stage.Stage;

    public class RBExample extends Application {
        RadioButton rBtn;
        Button btn;

    @Override
    public void start(Stage primaryStage) {
        rBtn = new RadioButton();
        rBtn.setText("Select Me");
        rBtn.setOnAction(this::handleRBSelectedAction);
        btn = new Button();
        btn.setText("Push Me");
        btn.setOnAction(this::handleBPushedAction);

        VBox root = new VBox(2);
        root.getChildren().add(rBtn);
        root.getChildren().add(btn);
        Scene scene = new Scene(root, 150, 50);
        primaryStage.setTitle("RBExample");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private void handleRBSelectedAction(ActionEvent event) {
        if (rBtn.isSelected()) {
            System.out.println("RB Selected directly");
        }
    }
    private void handleBPushedAction(ActionEvent event) {
        rBtn.setSelected(true);
        System.out.println("RB Selected by button");
    }

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

As can be seen from this example the RadioButton event handler is not called if action

rBtn.setSelected(true);

is perfomed externally (from Button action handler in this case). So we don't need to disable and re-enable RadioButton event handler.

Concerning the need of setSelected(true) inside RadioButton event handler to ensure RadioButton is truly got selected, it depends on rest of code if here are some backround processes which could intercept direct RadioButton state change.

Yogene
  • 31
  • 4
  • don't quite understand: is this the answer to your question? If no, don't post it in the answer section, instead edit your question and insert it. If yes, I don't understand the question nor the answer – kleopatra Aug 27 '18 at 13:23
  • Yes, it's an answer: if we turn RadioButton selected from outside, it's own event handler is not called. No need to disable it. – Yogene Aug 27 '18 at 13:25
  • It says I can't accept my own answer until tomorrow – Yogene Aug 28 '18 at 09:44