首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >JavaFX在listview中显示字符串附近的图像

JavaFX在listview中显示字符串附近的图像
EN

Stack Overflow用户
提问于 2015-12-30 18:25:42
回答 1查看 4.1K关注 0票数 0

我想在列表视图中显示一个字符串附近的消息,我试图查找它,但我无法理解它--我在控件/列表-view.htm网站上尝试过这样做--例如11-4创建一个细胞工厂--我尝试将它转换为imageview,但问题是我没有看到字符串,图像不是在字符串附近,图像太大了,应该有一种方法可以帮助我在列表视图中显示字符串附近的图像吗?这是我试图转换的代码:

第1段

代码语言:javascript
复制
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);
            }
        }
    }

第2部分

代码语言:javascript
复制
FileExplorerFormSlaveFileListView.setCellFactory(new Callback<ListView<String>, 
                ListCell<String>>() {

                    public ListCell<String> call(ListView<String> list) {
                        return new ColorRectCell();
                    }
                }
            );

我希望有人能帮我,这对我很重要。谢谢。如果你不明白我在问什么,告诉我,我会尝试格式化这个问题,我不善于解释问题。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-12-30 20:40:38

单元格贴标节点,可以天生地显示文本和图形,其中文本是任意图形节点的标签。因此,在单元格中,维护图形( ImageView)的呈现,并在updateItem实现中酌情设置图形和文本。

代码语言:javascript
复制
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构造函数中被调整大小,因此它们都是相同的高度。此外,该实现适合于文件类型图标的显示,因为将只创建任何给定文件类型的单个图像,并且可以通过在不同单元格中使用的不同ImageViews重用相同的图像。

代码语言:javascript
复制
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    

}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34535024

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档