Java Reference
In-Depth Information
javafx.scene.control.Labeled
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.ButtonBase
-onAction: ObjectProperty<EventHandler
<ActionEvent>>
Defines a handler for handling a button's action.
javafx.scene.control.CheckBox
-selected: BooleanProperty
Indicates whether this check box is checked.
+CheckBox()
+CheckBox(text: String)
Creates an empty check box.
Creates a check box with the specified text.
F
IGURE
16.7
CheckBox
contains the properties inherited from
ButtonBase
and
Labeled
.
When a check box is clicked (checked or unchecked), it fires an
ActionEvent
. To see if
a check box is selected, use the
isSelected()
method.
We now write a program that adds two check boxes named Bold and Italic to the pre-
ceding example to let the user specify whether the message is in bold or italic, as shown in
FigureĀ 16.8.
VBox
containing
two check
boxes
F
IGURE
16.8
The program demonstrates check boxes.
There are at least two approaches to writing this program. The first is to revise the preced-
ing
ButtonDemo
class to insert the code for adding the check boxes and processing their
events. The second is to define a subclass that extends
ButtonDemo
. Please implement the
first approach as an exercise. Listing 16.3 gives the code to implement the second approach.
L
ISTING
16.3
CheckBoxDemo.java
1
import
javafx.event.ActionEvent;
2
import
javafx.event.EventHandler;
3
import
javafx.geometry.Insets;
4
import
javafx.scene.control.CheckBox;
5
import
javafx.scene.layout.BorderPane;
6
import
javafx.scene.layout.VBox;
7
import
javafx.scene.text.Font;
8
import
javafx.scene.text.FontPosture;
9
import
javafx.scene.text.FontWeight;
10
11
public class
CheckBoxDemo
extends
ButtonDemo {
12 @Override
// Override the getPane() method in the super class
13
protected
BorderPane getPane() {
14 BorderPane pane =
super
.getPane();
Application
ButtonDemo
CheckBoxDemo
override
getPane()
invoke
super.getPane()
Search WWH ::
Custom Search