如何在javaFX中使用单选按钮

时间:2015-02-23 16:30:44

标签: java javafx radio-button toggle

我尝试使用单选按钮进行选择,然后一旦选择了一个选项,我就可以继续使用按钮运行某个与所选单选按钮匹配的方法。

以下是我想做的一个例子:

final ToggleGroup group = new ToggleGroup();
    RadioButton today = new RadioButton("Today");
    today.setToggleGroup(group);
    today.setSelected(true);
    RadioButton yesterday = new RadioButton("Yesterday");
    yesterday.setToggleGroup(group);
    RadioButton duration = new RadioButton("duration");
    duration.setToggleGroup(group);

    group.selectedToggleProperty().addListener(new ChangeListener<Toggle>() {
        @Override
        public void changed(ObservableValue<? extends Toggle> ov, Toggle t, Toggle t1) {

            RadioButton chk = (RadioButton)t1.getToggleGroup().getSelectedToggle(); // Cast object to radio button
            if(chk.getText() == "Today"){
              System.out.println("You've Selected Radio button 'Today'");
            }
            if(chk.getText() == "Yesterday"){
              System.out.println("You've Selected Radio button 'Yesterday'");
            } else{
                   System.out.println("You've Selected Radio button 'duration'");
            }
        }
    });

感谢您的帮助:)

0 个答案:

没有答案