在我的javafx项目代码就是这样。如何得到radioButton字符串值我需要outbut就是这样。我今天会点击,输出打印Today
就是这样一个
如何从group.selectedToggleProperty().addListener(new ChangeListener<Toggle>()
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.RadioButton;
import javafx.scene.control.Toggle;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
/**
*
* @author reegan
*/
public class GetSelectedToggleButton extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) {
Scene scene = new Scene(new Group());
stage.setTitle("Toggle Button Sample");
stage.setWidth(250);
stage.setHeight(180);
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) {
System.out.println(t1);
System.out.println(group.selectedToggleProperty().toString());
}
});
HBox hbox = new HBox();
hbox.getChildren().add(today);
hbox.getChildren().add(yesterday);
hbox.getChildren().add(duration);
((Group) scene.getRoot()).getChildren().add(hbox);
stage.setScene(scene);
stage.show();
}
}
答案 0 :(得分:15)
获取单选按钮类型转换侦听器对象的值到单选按钮。
试试这个..
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
System.out.println("Selected Radio Button - "+chk.getText());
}
});
答案 1 :(得分:0)
如果切换组由单选按钮组成,则可以通过以下方式找到所选单选按钮的值
String supplementation = ((RadioButton)supplememntation_toggle_group.getSelectedToggle()).getText();