JavaFX在listview中显示字符串附近的图像
我想在listview中的字符串附近显示一条消息我试图查找它,但我不太明白我在示例11-4的网站上尝试过创建一个单元格工厂我试图将其转换为imageview,但它确实起了作用,但问题是我看不到字符串,图像不在字符串附近,图像太大,应该有办法调整它的大小,所以有人能帮我在listview中显示字符串附近的图像吗?这是我试图转换的代码: 作品1JavaFX在listview中显示字符串附近的图像,java,image,listview,javafx,imageview,Java,Image,Listview,Javafx,Imageview,我想在listview中的字符串附近显示一条消息我试图查找它,但我不太明白我在示例11-4的网站上尝试过创建一个单元格工厂我试图将其转换为imageview,但它确实起了作用,但问题是我看不到字符串,图像不在字符串附近,图像太大,应该有办法调整它的大小,所以有人能帮我在listview中显示字符串附近的图像吗?这是我试图转换的代码: 作品1 static class ColorRectCell extends ListCell<String> { Image file
static class ColorRectCell extends ListCell<String> {
Image fileimg = new Image(getClass().getResourceAsStream("file.png"));
@Override
public void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
ImageView rect = new ImageView();
if (item != null) {
rect.setImage(fileimg);
setGraphic(rect);
}
}
}
静态类ColorRectCell扩展了ListCell{
Image fileimg=新图像(getClass().getResourceAsStream(“file.png”);
@凌驾
public void updateItem(字符串项,布尔值为空){
super.updateItem(项,空);
ImageView rect=新建ImageView();
如果(项!=null){
rect.setImage(fileimg);
设置图形(rect);
}
}
}
作品2
FileExplorerFormSlaveFileListView.setCellFactory(new Callback<ListView<String>,
ListCell<String>>() {
public ListCell<String> call(ListView<String> list) {
return new ColorRectCell();
}
}
);
fileexplorerPerformSlaveFileListView.setCellFactory(新回调(){
公共ListCell调用(ListView列表){
返回新的ColorRectCell();
}
}
);
我希望有人能帮助我,这对我很重要。谢谢如果您不能理解我的问题,请告诉我,我将尝试设置我不擅长解释问题的问题的格式。是可以天生显示文本和图形的节点,其中文本是任意图形节点的标签。因此,在单元格中,维护图形的渲染(ImageView),并在实现中适当地设置图形和文本
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样式的图像缓存,以便在后台按需加载较新的图像,在后台加载过程运行时,可能需要为图像添加占位符或进度指示器
在示例应用程序中,图像在图像构造函数中调整大小,使其高度相同。此外,该实现还适合于文件类型图标显示,因为任何给定文件类型都只会创建一个图像,并且可以通过不同单元格中使用的不同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
}
导入javafx.application.application;
导入javafx.collections.*;
导入javafx.geometry.Insets;
导入javafx.scene.scene;
导入javafx.scene.control.*;
导入javafx.scene.image.*;
导入javafx.scene.layout.VBox;
导入javafx.stage.stage;
导入java.util.Map;
导入java.util.stream.collector;
公共类标签列表扩展了应用程序{
专用静态最终双图像_高度=36;
私有静态最终字符串短前缀=
“鸟”;
私有静态最终字符串长前缀=
"http://icons.iconarchive.com/icons/jozef89/origami-birds/72/“+短_前缀;
私有静态最终字符串后缀=
“-icon.png”;
私有静态最终ObservableList=FXCollections.unmodifiableObservableList(
FXCollections.observableArrayList(
“-黑色”,
“-蓝色”,
“-红色”,
“-红色-2”,
“-黄色”,
“s-绿色”,
“s-green-2”
)
);
私人地图影像收藏;
@凌驾
public void start(Stage)引发异常{
imageCollection=birds.stream().collect(
汤姆(
鸟->鸟,
小鸟->新形象(
构造标签(长前缀、鸟、后缀),
0,
图像高度,
是的,
真的
)
)
);
ListView birdList=新的ListView(鸟类);
setCellFactory(param->newBirdCell());
鸟栏宽度(230);
鸟巢高度(200);
VBox布局=新的VBox(鸟列表);
布局。设置填充(新插图(10));
舞台场景(新场景(布局));
stage.show();
}
公共静态void main(字符串[]args){
发布(LabeledList.class);
}
私有类BirdCell扩展了ListCell{
private ImageView ImageView=新建ImageView();
@凌驾
受保护的void updateItem(字符串项,布尔值为空){
super.updateItem(项,空);
if(空| |项==null){
setImage(空);
设置图形(空);
setText(空);
}否则{
imageView.setImage(
imageCollection.get(
项目
)
);
setText(constructLabel(短前缀、项目、后缀));
设置图形(图像视图);
}
}
}
私有字符串标签(字符串前缀、字符串鸟、字符串后缀){
返回(前缀!=null?前缀:“”)
+鸟
+(后缀!=null?后缀:“”);
}
//Iconset主页:http://jozef89.deviantart.com/art/Origami-Birds-400642253
//许可证:CC归属非商业性非衍生产品3.0
//商业用途:不允许
}
是可以固有地显示文本和图形的节点,其中文本是任意图形节点的标签。因此,在单元格中,维护图形的渲染(ImageView),并在实现中适当地设置图形和文本
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);
}
}
示例应用程序
这里,所有可能的图像都预加载并存储在