Java Reference
In-Depth Information
15
16 Font fontBoldItalic = Font.font(
"Times New Roman"
,
17 FontWeight.BOLD, FontPosture.ITALIC,
20
);
18 Font fontBold = Font.font(
"Times New Roman"
,
19 FontWeight.BOLD, FontPosture.REGULAR,
20
);
20 Font fontItalic = Font.font(
"Times New Roman"
,
21 FontWeight.NORMAL, FontPosture.ITALIC,
20
);
22 Font fontNormal = Font.font(
"Times New Roman"
,
23 FontWeight.NORMAL, FontPosture.REGULAR,
20
);
24
25 text.setFont(fontNormal);
26
27 VBox paneForCheckBoxes =
new
VBox(
20
);
28 paneForCheckBoxes.setPadding(
new
Insets(
5
,
5
,
5
,
5
));
29 paneForCheckBoxes.setStyle(
"-fx-border-color: green"
);
30 CheckBox chkBold =
new
CheckBox(
"Bold"
);
31 CheckBox chkItalic =
new
CheckBox(
"Italic"
);
32 paneForCheckBoxes.getChildren().addAll(chkBold, chkItalic);
33 pane.setRight(paneForCheckBoxes);
34
35 EventHandler<ActionEvent> handler = e -> {
36
if
(chkBold.isSelected() && chkItalic.isSelected()) {
37 text.setFont(fontBoldItalic);
// Both check boxes checked
38 }
39
else if
(chkBold.isSelected()) {
40 text.setFont(fontBold);
// The Bold check box checked
41 }
42
else if
(chkItalic.isSelected()) {
43 text.setFont(fontItalic);
// The Italic check box checked
44 }
45
else
{
46 text.setFont(fontNormal);
// Both check boxes unchecked
47 }
48 };
49
50
create fonts
pane for check boxes
create check boxes
create a handler
chkBold.setOnAction(handler);
set handler for action
51
chkItalic.setOnAction(handler);
52
53
return
pane;
// Return a new pane
return a pane
54 }
55 }
main method omitted
CheckBoxDemo
extends
ButtonDemo
and overrides the
getPane()
method (line 13).
The new
getPane()
method invokes the
super.getPane()
method from the
ButtonDemo
class to obtain a border pane that contains the buttons and a text (line 14). The check boxes are
created and added to
paneForCheckBoxes
(lines 30-32).
paneForCheckBoxes
is added to
the border pane (lines 33).
The handler for processing the action event on check boxes is created in lines 35-48. It sets
the appropriate font based on the status of the check boxes.
The
start
method for this JavaFX program is defined in
ButtonDemo
and inherited
in
CheckBoxDemo
. So when you run
CheckBoxDemo
, the
start
method in
ButtonDemo
is invoked. Since the
getPane()
method is overridden in
CheckBoxDemo
, the method in
CheckBoxDemo
is invoked from line 41 in Listing 16.2, ButtonDemo.java.
16.8
✓
✓
How do you test if a check box is selected?
Check
16.9
Point
Can you apply all the methods for
Labeled
to
CheckBox
?
16.10
Can you set a node for the
graphic
property in a check box?
Search WWH ::
Custom Search