Java Reference
In-Depth Information
Radio buttons, also known as
option buttons
, enable the user to choose a single item from a
group of choices. In appearance radio buttons resemble check boxes, but check boxes display
a square that is either checked or blank, whereas radio buttons display a circle that is either
filled (if selected) or blank (if not selected).
RadioButton
is a subclass of
ToggleButton
. The difference between a radio button
and a toggle button is that a radio button displays a circle, but a toggle button is rendered
similar to a button. The UML diagrams for
ToggleButton
and
RadioButton
are shown in
FigureĀ 16.9.
option buttons
The getter and setter methods for property
values and a getter for property itself are provided
in the class, but omitted in the UML diagram for brevity.
javafx.scene.control.ToggleButton
-selected: BooleanProperty
-toggleGroup:
ObjectProperty<ToggleGroup>
Indicates whether the button is selected.
Specifies the button group to which the button belongs.
+ToggleButton()
+ToggleButton(text: String)
+ToggleButton(text: String, graphic: Node)
Creates an empty toggle button.
Creates a toggle button with the specified text.
Creates a toggle button with the specified text and graphic.
javafx.scene.control.RadioButton
+RadioButton()
+RadioButton(text: String)
Creates an empty radio button.
Creates a radio button with the specified text.
F
IGURE
16.9
ToggleButton
and
RadioButton
are specialized buttons for making selections.
Here is an example of a radio button with text
US
, a graphic image, green text color, and
black border, and initially selected.
RadioButton rbUS =
new
RadioButton(
"US"
);
rbUS.setGraphic(
new
ImageView(
"image/usIcon.gif"
));
rbUS.setTextFill(Color.GREEN);
rbUS.setContentDisplay(ContentDisplay.LEFT);
rbUS.setStyle(
"-fx-border-color: black"
);
rbUS.setSelected(
true
);
rbUS.setPadding(
new
Insets(
5
,
5
,
5
,));
To group radio buttons, you need to create an instance of
ToggleGroup
and set a radio
button's
toggleGroup
property to join the group, as follows:
ToggleGroup group =
new
ToggleGroup();
rbRed.setToggleGroup(group);
rbGreen.setToggleGroup(group);
rbBlue.setToggleGroup(group);
This code creates a button group for radio buttons
rbRed
,
rbGreen
, and
rbBlue
so that
buttons
rbRed
,
rbGreen
, and
rbBlue
are selected mutually exclusively. Without grouping,
these buttons would be independent.
When a radio button is changed (selected or deselected), it fires an
ActionEvent
. To see
if a radio button is selected, use the
isSelected()
method.
Search WWH ::
Custom Search