Cells https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/ListCell.html are Labeled https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/Labeled.html可以固有地显示文本和图形的节点,其中文本是任意图形节点的标签。因此,在您的单元格中,维护图形(ImageView)的渲染,并在更新项目 https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/Cell.html#updateItem-T-boolean-执行。
private ImageView imageView = new ImageView();
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (empty || item == null) {
imageView.setImage(null);
setGraphic(null);
setText(null);
} else {
imageView.setImage(
imageCollection.get(
item
)
);
setText(constructLabel(SHORT_PREFIX, item, SUFFIX));
setGraphic(imageView);
}
}
申请样本
在这里,所有可能的图像都被预先加载并存储在缓存中,如果图像数量较少,这将可以正常工作。如果您有大量图像,您可能需要更复杂的 LRU 样式缓存,以便在后台按需加载较新的图像,在后台加载过程运行时可能会为图像提供占位符或进度指示器。
在示例应用程序中,图像在 Image 构造函数中调整大小,以便它们具有相同的高度。此外,该实现适合文件类型图标显示,因为对于任何给定文件类型只会创建单个图像,并且可以通过不同单元格中使用的不同 ImageView 重用该相同图像。
import javafx.application.Application;
import javafx.collections.*;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.*;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.util.Map;
import java.util.stream.Collectors;
public class LabeledList extends Application {
private static final double IMAGE_HEIGHT = 36;
private static final String SHORT_PREFIX =
"bird";
private static final String LONG_PREFIX =
"http://icons.iconarchive.com/icons/jozef89/origami-birds/72/" + SHORT_PREFIX;
private static final String SUFFIX =
"-icon.png";
private static final ObservableList<String> birds = FXCollections.unmodifiableObservableList(
FXCollections.observableArrayList(
"-black",
"-blue",
"-red",
"-red-2",
"-yellow",
"s-green",
"s-green-2"
)
);
private Map<String, Image> imageCollection;
@Override
public void start(Stage stage) throws Exception {
imageCollection = birds.stream().collect(
Collectors.toMap(
bird -> bird,
bird -> new Image(
constructLabel(LONG_PREFIX, bird, SUFFIX),
0,
IMAGE_HEIGHT,
true,
true
)
)
);
ListView<String> birdList = new ListView<>(birds);
birdList.setCellFactory(param -> new BirdCell());
birdList.setPrefWidth(230);
birdList.setPrefHeight(200);
VBox layout = new VBox(birdList);
layout.setPadding(new Insets(10));
stage.setScene(new Scene(layout));
stage.show();
}
public static void main(String[] args) {
launch(LabeledList.class);
}
private class BirdCell extends ListCell<String> {
private ImageView imageView = new ImageView();
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (empty || item == null) {
imageView.setImage(null);
setGraphic(null);
setText(null);
} else {
imageView.setImage(
imageCollection.get(
item
)
);
setText(constructLabel(SHORT_PREFIX, item, SUFFIX));
setGraphic(imageView);
}
}
}
private String constructLabel(String prefix, String bird, String suffix) {
return (prefix != null ? prefix : "")
+ bird
+ (suffix != null ? suffix : "");
}
// Iconset Homepage: http://jozef89.deviantart.com/art/Origami-Birds-400642253
// License: CC Attribution-Noncommercial-No Derivate 3.0
// Commercial usage: Not allowed
}