Java Reference
In-Depth Information
38 }
39 });
40
41 rbBlue.setOnAction(e -> {
42
if
(rbBlue.isSelected()) {
43 text.setFill(Color.BLUE);
44 }
45 });
46
47
return
pane;
return border pane
48 }
49 }
main method omitted
RadioButtonDemo
extends
CheckBoxDemo
and overrides the
getPane()
method (line
10). The new
getPane()
method invokes the
getPane()
method from the
CheckBoxDemo
class to create a border pane that contains the check boxes, buttons, and a text (line 11). This
border pane is returned from invoking
super.getPane()
. The radio buttons are created and
added to
paneForRadioButtons
(lines 18-21).
paneForRadioButtons
is added to the
border pane (lines 22).
The radio buttons are grouped together in lines 24-27. The handlers for processing the
action event on radio buttons are created in lines 29-45. It sets the appropriate color based on
the status of the radio buttons.
The
start
method for this JavaFX program is defined in
ButtonDemo
and inherited in
CheckBoxDemo
and then in
RadioButtonDemo
. So when you run
RadioButtonDemo
,
the
start
method in
ButtonDemo
is invoked. Since the
getPane()
method is overrid-
den in
RadioButtonDemo
, the method in
RadioButtonDemo
is invoked from line 41 in
Listing 16.2, ButtonDemo.java.
16.11
✓
✓
How do you test if a radio button is selected?
Check
16.12
Can you apply all the methods for
Labeled
to
RadioButton
?
Point
16.13
Can you set any node in the
graphic
property in a radio button?
16.14
How do you group radio buttons?
A text field can be used to enter or display a string.
TextField
is a subclass of
TextInputControl
. Figure 16.11 lists the properties and constructors in
TextField
.
Here is an example of creating a noneditable text field with red text color, a specified font,
and right horizontal alignment:
TextField tfMessage =
new
TextField(
"T-Strom"
);
tfMessage.setEditable(
false
);
tfMessage.setStyle(
"-fx-text-fill: red"
);
tfMessage.setFont(Font.font(
"Times"
,
20
));
tfMessage.setAlignment(Pos.BASELINE_RIGHT);
When you move the cursor in the text field and press the Enter key, it fires an
ActionEvent
.
Listing 16.5 gives a program that adds a text field to the preceding example to let the user
set a new message, as shown in Figure 16.12.
L
ISTING
16.5
TextFieldDemo.java
1
import
javafx.geometry.Insets;
2
import
javafx.geometry.Pos;
Search WWH ::
Custom Search