【问题标题】:How to assign a hidden integer value to a RadioButton?如何将隐藏的整数值分配给 RadioButton?
【发布时间】:2018-10-29 21:03:14
【问题描述】:

例如,我在 ToggleGroup 中有四个 RadioButtons,整数值为 0、1、2、3。当用户点击一个单选按钮时,我想获取它的值。

我想使用解决方案来获取该值以用作列表的索引。

例如: 用户单击第三个单选按钮。该 RadioButton 的值必须为 2。

如果有任何其他方法可以为 radioButton 分配重要的值,我很乐意听到。谢谢

【问题讨论】:

    标签: javafx radio-button controls


    【解决方案1】:

    您实际上有几个选项,您选择哪一个将取决于您想要的功能。我将在下面展示每个选项的代码。

    1. 使用toggleGroup.getToggles() 列表 - 这已经为ObservableList<Toggle> 中的所有元素提供了索引
    2. 为每个RadioButton添加一个UserData属性
    3. 创建您自己的RadioButton - 这将允许添加任意数量的属性,类似于选项 2,但代码更简洁(在我看来)
    4. 创建您自己的属性绑定到所选RadioButton的索引

    使用toggleGroup.getToggles() 列表

    如果您将每个 RadioButton 添加到 ToggleGroup,则它们已经在列表中。您可以使用 toggleGroup.getToggles() 访问该列表。然后,您可以简单地在列表中获取它们的索引。

    import javafx.application.Application;
    import javafx.geometry.Insets;
    import javafx.geometry.Pos;
    import javafx.scene.Scene;
    import javafx.scene.control.RadioButton;
    import javafx.scene.control.Toggle;
    import javafx.scene.control.ToggleGroup;
    import javafx.scene.layout.VBox;
    import javafx.stage.Stage;
    
    public class Main extends Application {
    
        public static void main(String[] args) {
            launch(args);
        }
    
        @Override
        public void start(Stage primaryStage) {
    
            // Simple interface
            VBox root = new VBox(10);
            root.setPadding(new Insets(10));
            root.setAlignment(Pos.CENTER);
    
            // Create the ToggleGroup
            ToggleGroup group = new ToggleGroup();
    
            // Add some RadioButtons to the group
            group.getToggles().addAll(
                    new RadioButton("One"),
                    new RadioButton("Two"),
                    new RadioButton("Three"),
                    new RadioButton("Four"),
                    new RadioButton("Five")
            );
    
            // Add all the RadioButtons to the scene
            for (Toggle radioButton : group.getToggles()) {
                root.getChildren().add((RadioButton) radioButton);
            }
    
            // Now we can get the index any time a RadioButton is selected
            group.selectedToggleProperty().addListener((observable, oldValue, newValue) -> {
                if (newValue != null) {
                    System.out.println("Index #" + group.getToggles().indexOf(newValue) + " selected!");
                }
            });
    
            // Show the Stage
            primaryStage.setScene(new Scene(root));
            primaryStage.show();
        }
    }
    

    为每个RadioButton添加一个UserData属性

    由于RadioButtonNode,它继承了一个有用的便捷方法[setUserData()][1]。这允许您将单个 Object 属性添加到 Node 以供以后检索。

    如果您只想添加这个值,这是一个可行的选择,并且使用起来非常简单。对于每个RadioButton,只需调用该方法并传递一个Object

    rdo1.setUserData(1);
    rdo2.setUserData(2);
    rdo3.setUserData(3);
    rdo4.setUserData(4);
    rdo5.setUserData(5);
    

    当您需要时,只需调用getUserData() 方法即可检索该值。

    当然,这里的危险在于它不是真正的类型安全的。由于您可以将任何 Java Object 传递给此方法(并会得到一个 Object 作为回报),因此您需要严格控制此处设置的内容并在检索时正确转换:

    int radio1Value = (int) rdo1.getUserData();
    

    创建您自己的RadioButton

    但是,如果您希望能够为每个 RadioButton 手动分配一个属性值,并且可能包含多个自定义值,您可以简单地创建一个扩展 RadioButton 的类并提供附加属性(即):

    class MyRadioButton extends RadioButton {
    
        private int myIndex;
    
        public MyRadioButton(String text, int myIndex) {
            super(text);
            this.myIndex = myIndex;
        }
    
        public int getMyIndex() {
            return myIndex;
        }
    
        public void setMyIndex(int myIndex) {
            this.myIndex = myIndex;
        }
    }
    

    创建您自己的属性绑定到所选 RadioButton 的索引

    同样,如果您只对跟踪所选RadioButton 的索引感兴趣,您可以创建一个IntegerProperty 并将其绑定到一个IntegerBinding,该IntegerBinding 会在RadioButton 选择更改时更新。

    // Create a property to hold the index value of the currently selected RadioButton
    IntegerBinding selectedRadioButtonIndexBinding = Bindings.createIntegerBinding(() ->
                    group.getToggles().indexOf(group.getSelectedToggle()), group.getToggles(), group.selectedToggleProperty());
    IntegerProperty selectedRadioButtonIndex = new SimpleIntegerProperty();
    selectedRadioButtonIndex.bind(selectedRadioButtonIndexBinding);
    

    此时,您只需在新属性中添加Listener

    selectedRadioButtonIndex.addListener((observable, oldValue, newValue) -> {
        System.out.println(newValue);
    });
    

    【讨论】:

    • 扩展RadioButton 的替代方法是使用userDataproperties 来存储值。
    • Bindings.createIntegerBinding(() -> group.getToggles().indexOf(group.getSelectedToggle()), group.getToggles(), group.selectedToggleProperty())
    • 感谢@fabian 和@Jai 的替代方案!我已经更新了我的答案以包含它们。
    猜你喜欢
    • 2012-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多