Java Reference
In-Depth Information
L
ISTING
16.7
TextAreaDemo.java
1
import
javafx.application.Application;
2
import
javafx.stage.Stage;
3
import
javafx.scene.Scene;
4
import
javafx.scene.image.ImageView;
5
6
public class
TextAreaDemo
extends
Application {
7 @Override
// Override the start method in the Application class
8
public void
start(Stage primaryStage) {
9
// Declare and create a description pane
10
DescriptionPane descriptionPane =
new
DescriptionPane();
create
descriptionPane
11
12
// Set title, text, and image in the description pane
13 descriptionPane.setTitle(
"Canada"
);
14 String description =
"The Canadian national flag ..."
;
15 descriptionPane.setImageView(
new
ImageView(
"image/ca.gif"
));
16 descriptionPane.setDescription(description);
17
18
// Create a scene and place it in the stage
19 Scene scene =
new
Scene(descriptionPane,
450
,
200
);
20 primaryStage.setTitle(
"TextAreaDemo"
);
// Set the stage title
21 primaryStage.setScene(scene);
// Place the scene in the stage
22 primaryStage.show();
// Display the stage
23 }
24 }
set title
set image
add
descriptionPane
to scene
The program creates an instance of
DescriptionPane
(line 10), and sets the title (line 13),
image (line 15), and text in the description pane (line 16).
DescriptionPane
is a subclass of
Pane
.
DescriptionPane
contains a label for displaying an image and a title, and a text area
for displaying a description of the image.
16.19
✓
✓
How do you create a text area with
10
rows and
20
columns?
Check
16.20
How do you obtain the text from a text area?
Point
16.21
Can you disable editing of a text area?
16.22
What method do you use to wrap text to the next line in a text area?
A combo box, also known as a choice list or drop-down list, contains a list of items
from which the user can choose.
Key
Point
A combo box is useful for limiting a user's range of choices and avoids the cumbersome vali-
dation of data input. Figure 16.16 lists several frequently used properties and constructors in
ComboBox
.
ComboBox
is defined as a generic class. The generic type
T
specifies the element
type for the elements stored in a combo box.
The following statements create a combo box with four items, red color, and value set to
the first item.
ComboBox<String> cbo =
new
ComboBox<>();
cbo.getItems().addAll(
"Item 1"
,
"Item 2"
,
"Item 3"
,
"Item 4"
);
cbo.setStyle(
"-fx-color: red"
);
cbo.setValue(
"Item 1"
);
Search WWH ::
Custom Search