javafx
ラジオボタン
サーチ…
ラジオボタンの作成
ラジオボタンを使用すると、ユーザーは与えられた要素の1つの要素を選択できます。 RadioButtonは、それ以外のテキストを宣言する方法が2つあります。デフォルトのコンストラクタであるRadioButton()を使用し、 setText(String)メソッドでテキストを設定するか、別のコンストラクタRadioButton(String)を使用して設定します。
RadioButton radioButton1 = new RadioButton();
radioButton1.setText("Select me!");
RadioButton radioButton2= new RadioButton("Or me!");
RadioButtonはLabeled拡張であるため、 RadioButton指定されたImage使用することもできRadioButton 。コンストラクタの1つでRadioButtonを作成した後は、 setGraphic(ImageView)ようにsetGraphic(ImageView)メソッドをsetGraphic(ImageView)てImageを追加します。
Image image = new Image("ok.jpg");
RadioButton radioButton = new RadioButton("Agree");
radioButton.setGraphic(new ImageView(image));
ラジオボタンでグループを使用する
ToggleGroupはRadioButtonを管理するために使用され、毎回各グループの1つだけを選択できます。
次のような簡単なToggleGroup作成します。
ToggleGroup group = new ToggleGroup();
Togglegroupを作成した後、 Togglegroup setToggleGroup(ToggleGroup)を使用してRadioButton割り当てることができます。 setSelected(Boolean)を使用して、 RadioButtonの1つを事前に選択します。
RadioButton radioButton1 = new RadioButton("stackoverlow is awesome! :)");
radioButton1.setToggleGroup(group);
radioButton1.setSelected(true);
RadioButton radioButton2 = new RadioButton("stackoverflow is ok :|");
radioButton2.setToggleGroup(group);
RadioButton radioButton3 = new RadioButton("stackoverflow is useless :(");
radioButton3.setToggleGroup(group);
ラジオボタンのイベント
通常、 ToggleGroup RadioButtonの1つが選択されると、アプリケーションはアクションを実行します。以下は、 setUserData(Object)設定さRadioButtonた選択したRadioButtonユーザデータを出力する例です。
radioButton1.setUserData("awesome")
radioButton2.setUserData("ok");
radioButton3.setUserData("useless");
ToggleGroup group = new ToggleGroup();
group.selectedToggleProperty().addListener((obserableValue, old_toggle, new_toggle) -> {
if (group.getSelectedToggle() != null) {
System.out.println("You think that stackoverflow is " + group.getSelectedToggle().getUserData().toString());
}
});
ラジオボタンのフォーカスを要求する
3つのうち2つ目のRadioButtonがsetSelected(Boolean)であらかじめ選択されているとします。デフォルトではフォーカスは最初のRadioButton残ります。これを変更するには、 requestFocus()メソッドを使用します。
radioButton2.setSelected(true);
radioButton2.requestFocus();