-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageTextCell.java
More file actions
48 lines (41 loc) · 1.83 KB
/
Copy pathImageTextCell.java
File metadata and controls
48 lines (41 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package sample;
// Fig. 13.16: ImageTextCell.java
// Custom ListView cell factory that displays an Image and text
import javafx.geometry.Pos;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.VBox;
import javafx.scene.text.TextAlignment;
public class ImageTextCell extends ListCell<Contacts> {
private VBox vbox = new VBox(8.0); // 8 points of gap between controls
private ImageView thumbImageView = new ImageView(); // initially empty
private Label label = new Label();
// constructor configures VBox, ImageView and Label
public ImageTextCell() {
vbox.setAlignment(Pos.CENTER); // center VBox contents horizontally
thumbImageView.setPreserveRatio(true);
thumbImageView.setFitHeight(100.0); // thumbnail 100 points tall
vbox.getChildren().add(thumbImageView); // attach to Vbox
label.setWrapText(true); // wrap if text too wide to fit in label
label.setTextAlignment(TextAlignment.CENTER); // center text
vbox.getChildren().add(label); // attach to VBox
setPrefWidth(USE_PREF_SIZE); // use preferred size for cell width
}
// called to configure each custom ListView cell
@Override
protected void updateItem(Contacts item, boolean empty) {
// required to ensure that cell displays properly
super.updateItem(item, empty);
if (empty || item == null) {
setGraphic(null); // don't display anything
}
else {
// set ImageView's thumbnail image
thumbImageView.setImage(new Image(item.getImagePath()));
label.setText(item.getFirst()+ " "+item.getLast()); // configure Label's text
setGraphic(vbox); // attach custom layout to ListView cell
}
}
}