英文:
Selecting a RadioButton in a ToggleGroup whenever it is navigated to with the arrow keys
问题
我有一个带有一系列共享ToggleGroup
的RadioButton
的程序。以下是一个简化版本:
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
ToggleGroup toggleGroup = new ToggleGroup();
RadioButton button1 = new RadioButton();
button1.setText("按钮1");
button1.setOnAction(this::printSelectedRadioButton);
button1.setToggleGroup(toggleGroup);
RadioButton button2 = new RadioButton();
button2.setText("按钮2");
button2.setOnAction(this::printSelectedRadioButton);
button2.setToggleGroup(toggleGroup);
VBox root = new VBox(10);
root.setAlignment(Pos.CENTER);
root.getChildren().addAll(button1, button2);
primaryStage.setScene(new Scene(root, 100, 100));
primaryStage.show();
}
private void printSelectedRadioButton(ActionEvent actionEvent) {
RadioButton radioButton = (RadioButton) actionEvent.getSource();
System.out.println(radioButton.getText());
}
public static void main(String[] args) {
launch(args);
}
}
当我点击一个RadioButton
时,会触发一个ActionEvent
并调用printSelectedRadioButton()
方法。然而,一旦我点击了一个RadioButton
,如果我使用箭头键导航到另一个按钮,ActionEvent
不会触发,方法也不会被调用。我希望导航到特定按钮具有与单击它相同的效果。我该如何做到这一点?
英文:
I have a program with a series of RadioButton
s that share a ToggleGroup
. The following is a simplified version:
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
ToggleGroup toggleGroup = new ToggleGroup();
RadioButton button1 = new RadioButton();
button1.setText("Button 1");
button1.setOnAction(this::printSelectedRadioButton);
button1.setToggleGroup(toggleGroup);
RadioButton button2 = new RadioButton();
button2.setText("Button 2");
button2.setOnAction(this::printSelectedRadioButton);
button2.setToggleGroup(toggleGroup);
VBox root = new VBox(10);
root.setAlignment(Pos.CENTER);
root.getChildren().addAll(button1, button2);
primaryStage.setScene(new Scene(root, 100, 100));
primaryStage.show();
}
private void printSelectedRadioButton(ActionEvent actionEvent) {
RadioButton radioButton = (RadioButton) actionEvent.getSource();
System.out.println(radioButton.getText());
}
public static void main(String[] args) {
launch(args);
}
}
When I click on a RadioButton
, an ActionEvent
is fired and the printSelectedRadioButton()
method is called. However, once I have clicked on a RadioButton
, if I navigate to the other one using the arrow keys, the ActionEvent
is not fired and the method is not called. I want navigating to a particular button to have the same effect as clicking on it. How would I do this?
答案1
得分: 2
RadioButton#setOnAction仅在点击时起作用。如果您想获取选中的单选按钮,您需要向ToggleGroup添加***ChangeListener***。
ToggleGroup toggleGroup = new ToggleGroup();
toggleGroup.selectedToggleProperty().addListener((observableValue, oldToggle, newToggle) -> {
if (toggleGroup.getSelectedToggle() != null) {
System.out.println("选中的单选按钮:" + toggleGroup.getSelectedToggle());
}
});
英文:
RadioButton#setOnAction works only on click. If you want to get selected radio button you have to add ChangeListener to ToggleGroup.
ToggleGroup toggleGroup = new ToggleGroup();
toggleGroup.selectedToggleProperty().addListener((observableValue, oldToggle, newToggle) -> {
if (toggleGroup.getSelectedToggle() != null) {
System.out.println("selected radio button: " + toggleGroup.getSelectedToggle());
}
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论