Java Reference
In-Depth Information
passwordField.focusedProperty().addListener((ov, oldValue, newValue) -> {
if (!passwordField.isFocused()) {
System.out.println("PasswordField text is: "
+ passwordField.getText());
}
});
To detect when the
PasswordField
has lost focus, the code in the preceding snippet adds a
ChangeListener
to
the
focused
property. The value of the
text
property is then printed to the Java console in the body of the lambda
expression if the
PasswordField
is indeed not focused.
Creating a
TextArea
The
TextArea
control is similar to the
TextField
control, but allows for multiple lines of text. In Step 27 of the exercise,
when you entered text into the
TextArea
shown in Figure
6-9
and subsequently caused the
TextArea
to lose focus,
the contents of the
TextArea
were printed to the Java console. The following snippet from Listing 6-12 creates the
TextArea
and implements these behaviors:
final TextArea textArea = new TextArea();
textArea.setPrefColumnCount(12);
textArea.setPrefRowCount(4);
textArea.focusedProperty().addListener((ov, oldValue, newValue) -> {
if (!textArea.isFocused()) {
System.out.println("TextArea text is: " + textArea.getText());
}
});
Some useful
TextArea
properties not demonstrated in the preceding snippet are:
•
wrapText
, which controls whether the text will wrap in the
TextArea
•
scrollLeft/scrollTop
, which are the number of pixels by which the content is horizontally
or vertically scrolled
Let's move away from the UI controls that accept text input, and briefly talk about two very convenient utilities.
Creating a
DatePicker
and a
ColorPicker
When JavaFX 2 was released with a number of controls, attention was paid to what else was needed in real-world
applications. Some new controls have been added in JavaFX 2.2 and JavaFX 8, based on input from the developer
community. A particularly useful control is the
DatePicker
, which allows users to open a calendar view and select a
particular date. The following snippet from Listing 6-12 shows how to include this control in a JavaFX application:
LocalDate today = LocalDate.now();
DatePicker datePicker = new DatePicker(today);
datePicker.setOnAction(e -> System.out.println("Selected date: " + datePicker.getValue()));
When the user clicks the date control shown in Figure
6-10
, a calendar will pop up. Selecting a date on the
calendar will trigger the
onAction
event handler on the
datePicker
instance, and the lambda expression will print the
selected date to the standard console.
Search WWH ::
Custom Search