我是否必须阻止循环操作处理程序调用
Do I have to prevent cycle action handler call
找不到合适的方法来找到现有的答案,所以这里是描述我的情况的草图示例:
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
}
}
我是否必须用 disable/enable 操作处理指令包围 rb.setSelected(true)?
handleSelectedAction(ActionEvent _selected) {
// DO if RadioButton rb is selected directly (by mouse etc.)
rb.setOnAction(null);
rb.setSelected(true);
rb.setOnAction(this::handleSelectedAction);
}
原始代码运行良好,但我怀疑 handleSelectedAction 方法是否永久 运行 在后台。
好的,伙计们,根据 kleopatra 的要求,我编写了一个简短的示例,可以是 运行:
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);
}
}
从这个例子可以看出,如果操作
,则不会调用 RadioButton 事件处理程序
rBtn.setSelected(true);
在外部执行(在本例中来自按钮操作处理程序)。
所以我们不需要禁用和重新启用 RadioButton 事件处理程序。
关于需要在 RadioButton 事件处理程序中使用 setSelected(true) 来确保 RadioButton 被真正选中,这取决于其余代码是否有一些可以拦截直接 RadioButton 状态更改的后台进程。
找不到合适的方法来找到现有的答案,所以这里是描述我的情况的草图示例:
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
}
}
我是否必须用 disable/enable 操作处理指令包围 rb.setSelected(true)?
handleSelectedAction(ActionEvent _selected) {
// DO if RadioButton rb is selected directly (by mouse etc.)
rb.setOnAction(null);
rb.setSelected(true);
rb.setOnAction(this::handleSelectedAction);
}
原始代码运行良好,但我怀疑 handleSelectedAction 方法是否永久 运行 在后台。
好的,伙计们,根据 kleopatra 的要求,我编写了一个简短的示例,可以是 运行:
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);
}
}
从这个例子可以看出,如果操作
,则不会调用 RadioButton 事件处理程序rBtn.setSelected(true);
在外部执行(在本例中来自按钮操作处理程序)。 所以我们不需要禁用和重新启用 RadioButton 事件处理程序。
关于需要在 RadioButton 事件处理程序中使用 setSelected(true) 来确保 RadioButton 被真正选中,这取决于其余代码是否有一些可以拦截直接 RadioButton 状态更改的后台进程。