Java Reference
In-Depth Information
4
import
javafx.scene.control.ScrollPane;
5
import
javafx.scene.control.TextArea;
6
import
javafx.scene.image.ImageView;
7
import
javafx.scene.layout.BorderPane;
8
import
javafx.scene.text.Font;
9
10
public class
DescriptionPane
extends
BorderPane {
11
/** Label for displaying an image and a title */
12
private
Label lblImageTitle =
new
Label();
label
13
14
/** Text area for displaying text */
15
private
TextArea taDescription =
new
TextArea();
text area
16
17
public
DescriptionPane() {
18
// Center the icon and text and place the text under the icon
19 lblImageTitle.setContentDisplay(ContentDisplay.TOP);
20 lblImageTitle.setPrefSize(
200
,
100
);
21
22
// Set the font in the label and the text field
23 lblImageTitle.setFont(
new
Font(
"SansSerif"
,
16
));
24 taDescription.setFont(
new
Font(
"Serif"
,
14
));
25
26 taDescription.setWrapText(
true
);
27 taDescription.setEditable(
false
);
28
29
label properties
wrap text
read only
// Create a scroll pane to hold the text area
30
ScrollPane scrollPane =
new
ScrollPane(taDescription);
scroll pane
31
32
// Place label and scroll pane in the border pane
33 setLeft(lblImageTitle);
34 setCenter(scrollPane);
35 setPadding(
new
Insets(
5
,
5
,
5
,
5
));
36 }
37
38
/** Set the title */
39
public void
setTitle(String title) {
40 lblImageTitle.setText(title);
41 }
42
43
/** Set the image view */
44
public void
setImageView(ImageView icon) {
45 lblImageTitle.setGraphic(icon);
46 }
47
48
/** Set the text description */
49
public void
setDescription(String text) {
50 taDescription.setText(text);
51 }
52 }
The text area is inside a
ScrollPane
(line 30), which provides scrolling functions for the
text area.
The
wrapText
property is set to
true
(line 26) so that the line is automatically wrapped
when the text cannot fit in one line. The text area is set as noneditable (line 27), so you cannot
edit the description in the text area.
It is not necessary to define a separate class for
DescriptionPane
in this example. How-
ever, this class was defined for reuse in the next section, where you will use it to display a
description pane for various images.
Search WWH ::
Custom Search