javafx Creating Radio Buttons


Radio Buttons allow you to let the user choose one element of those given. There are two ways to declare a RadioButton with a text besides it. Either by using the default constructor RadioButton() and setting the text with the setText(String) method or by using the other constructor RadioButton(String).

RadioButton radioButton1 = new RadioButton();
radioButton1.setText("Select me!");
RadioButton radioButton2= new RadioButton("Or me!");

As RadioButton is an extension of Labeled there can also be an Image specified to the RadioButton. After creating the RadioButton with one of the constructors simply add the Image with the setGraphic(ImageView) method like here:

Image image = new Image("ok.jpg");
RadioButton radioButton = new RadioButton("Agree");
radioButton.setGraphic(new ImageView(image));