当前位置: 首页 > 知识库问答 >
问题:

将图像添加到JavaFX TableView列中

章茂
2023-03-14

我不熟悉Java和OOP,一直在将图像添加到tableview列。代码似乎工作,我可以看到学生的名字正确,但图像没有显示在列中。我遇到了这个错误,无法理解如何使其工作:

javafx.scene.control.cell.PropertyValueFactory getCellDataReflectively
WARNING: Can not retrieve property 'picture' in PropertyValueFactory: javafx.scene.control.cell.PropertyValueFactory@5b0da50f with provided class type: class model.StudentModel
java.lang.IllegalStateException: Cannot read from unreadable property picture

学生模型:

package model;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.image.ImageView;

import java.util.ArrayList;
import java.util.List;

public class StudentModel {

    private ImageView picture;
    private String name;
    private SubjectModel major;
    private SubjectModel minor;
    private String accountPassword;
    public String getAccountPassword()
    {
        return accountPassword;
    }
    public List<LectureModel> lectureModelList = new ArrayList<>();

    public StudentModel(String name, SubjectModel major, SubjectModel minor, ImageView picture, String accountPassword)
    {
        this.name = name;
        this.major = major;
        this.minor = minor;
        this.picture = picture;
        this.accountPassword = accountPassword;
    }
    public String getName()
    {
        return name;
    }

    public ObservableList<LectureModel> myObservableLectures(){
        ObservableList<LectureModel> observableList = FXCollections.observableArrayList(lectureModelList);
        return observableList;
    }


    public ImageView getPhoto(){
        return picture;
    }


    public void setPhoto(ImageView photo)
    {
        this.picture =  photo;

    }
}

和参与者场景,我有桌面视图:

public class ParticipantsScene extends Scene {

    private final StudentController studentController;
    private final ClientApplication clientApplication;
    private final TableView<StudentModel> allParticipantsTable;
    private final ObservableList<StudentModel> enrolledStudents;
    private LectureModel lecture;

    public ParticipantsScene(StudentController studentController, ClientApplication application, LectureModel lecture) {
        super(new VBox(), 800 ,500);
        this.clientApplication = application;
        this.studentController = studentController;
        this.lecture = lecture;
        enrolledStudents=lecture.observeAllParticipants();


        TableColumn<StudentModel, String > nameCol = new TableColumn<>("Name");
        nameCol.setMinWidth(200);
        nameCol.setCellValueFactory(new PropertyValueFactory<>("name"));

        TableColumn<StudentModel, ImageView> picCol = new TableColumn<>("Images");
        picCol.setPrefWidth(200);
        picCol.setCellValueFactory(new PropertyValueFactory<>("picture"));

        allParticipantsTable = new TableView<>();
        allParticipantsTable.getColumns().addAll(nameCol,picCol);
        allParticipantsTable.setItems(enrolledStudents);

        VBox vBox = new VBox(10, allParticipantsTable, createButtonBox());
        vBox.setAlignment(Pos.CENTER);
        setRoot(vBox);

    }
    private HBox createButtonBox() {
        var backButton = new Button("Back");
        backButton.setOnAction(event -> clientApplication.showAllLecturesScene());

        var buttonBox = new HBox(10, backButton);
        buttonBox.setAlignment(Pos.CENTER);
        return buttonBox;
    }
}

如果可能有帮助,还可以添加讲座模型:

public class LectureModel {

    private String lectureName;
    private String lectureHall;
    private String subjectName;
    private SubjectModel subject;
    private TimeSlot timeSlot;
    //private Button actionButton1;
    //private Button actionButton2;

    private List<StudentModel> enrolledStudents = new ArrayList<>();
    private String name;



    public LectureModel(String lectureName, String lectureHall, SubjectModel subject, TimeSlot timeSlot){
        this.lectureName = lectureName;
        this.lectureHall = lectureHall;
        this.subject = subject;
        this.timeSlot = timeSlot;
        this.subjectName = this.subject.getSubjectName();
    }

    public String getLectureName()
    {
        return lectureName;
    }
    public String getLectureHall()
    {
        return lectureHall;
    }
    public SubjectModel getSubject()
    {
        return subject;
    }
    public String getSubjectName()
    {
        return subjectName;
    }
    public List<StudentModel> getEnrolledStudents()
    {
        return enrolledStudents;
    }

    public ObservableList<StudentModel> observeAllParticipants() {
        ObservableList<StudentModel> observableList = FXCollections.observableArrayList(getEnrolledStudents());
        return observableList;
    }
    public TimeSlot getTimeSlot() {
        return timeSlot;
    }

    public void addStudent(StudentModel studentModel){ enrolledStudents.add(studentModel);}
    public void removeStudent(StudentModel studentModel)
    {
        enrolledStudents.remove(studentModel);
    };

感谢您的帮助,谢谢!

共有1个答案

乐正心水
2023-03-14

您将PropertyValueFactory中使用的属性名称命名错误。

通常,不要使用PropertyValueFactorys,而是使用lambda:

  • 为什么我应该避免在JavaFX中使用PropertyValueFactory

此外,作为一般原则,将数据放置在模型中,而不是节点中。例如,在模型中存储图像或图像的URL,而不是ImageView。然后仅在模型视图中使用节点。例如,要在表格单元格中显示图像,请使用单元格工厂。

如果需要,可以将LRU缓存用于图像(可能不需要)。

通常,表中显示的图像可能比全尺寸图像小,例如缩略图。为了提高效率,您可能希望使用大小图像构造函数在后台加载图像。

如果需要帮助放置和定位图像资源,请参阅:

  • 如何确定我的JavaFX应用程序所需的FXML文件、CSS文件、图像和其他资源的正确路径

示例代码

本答案中的示例使用了答案文本中的一些原则:

  • 使用Lambda而不是PropertyValue
  • 如果你的应用程序需要,你可以跳过缩略图大小,使用全尺寸图像
  • 如果希望用户界面等待图像加载后再显示,则可以在前台加载(不推荐,但对于较小的局部图像,您不会看到任何差异)
  • 如果您没有很多图像(例如数千个),您可以将图像(而不是ImageView)直接存储在模型中并使用它,从解决方案中删除LRU缓存

虽然我没有测试它,但这个解决方案应该可以很好地扩展到一个包含数千行的表,每个行都有不同的图像。

此处提供了本回答中使用的图像:

  • JavaFX:带有自定义单元格工厂的组合框-错误渲染
import javafx.application.Application;
import javafx.beans.property.*;
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.*;

public class StudentTableViewer extends Application {
    public record Student(String last, String first, String avatar) {}

    @Override
    public void start(Stage stage) {
        TableView<Student> table = createTable();
        populateTable(table);

        VBox layout = new VBox(
                10,
                table
        );
        layout.setPadding(new Insets(10));
        layout.setPrefSize(340, 360);

        layout.setStyle("-fx-font-size:20px; -fx-base: antiquewhite");

        stage.setScene(new Scene(layout));
        stage.show();
    }

    private TableView<Student> createTable() {
        TableView<Student> table = new TableView<>();

        TableColumn<Student, String> lastColumn = new TableColumn<>("Last");
        lastColumn.setCellValueFactory(
                p -> new ReadOnlyStringWrapper(p.getValue().last()).getReadOnlyProperty()
        );

        TableColumn<Student, String> firstColumn = new TableColumn<>("First");
        firstColumn.setCellValueFactory(
                p -> new ReadOnlyStringWrapper(p.getValue().first()).getReadOnlyProperty()
        );

        TableColumn<Student, String> avatarColumn = new TableColumn<>("Avatar");
        avatarColumn.setCellValueFactory(
                p -> new ReadOnlyStringWrapper(p.getValue().avatar()).getReadOnlyProperty()
        );
        avatarColumn.setCellFactory(
                p -> new AvatarCell()
        );
        avatarColumn.setPrefWidth(70);

        //noinspection unchecked
        table.getColumns().addAll(lastColumn, firstColumn, avatarColumn);

        return table;
    }

    public static class AvatarCell extends TableCell<Student, String> {
        private final ImageView imageView = new ImageView();
        private final ImageCache imageCache = ImageCache.getInstance();

        @Override
        protected void updateItem(String url, boolean empty) {
            super.updateItem(url, empty);

            if (url == null || empty || imageCache.getThumbnail(url) == null) {
                imageView.setImage(null);
                setGraphic(null);
            } else {
                imageView.setImage(imageCache.getThumbnail(url));
                setGraphic(imageView);
            }
        }
    }

    private void populateTable(TableView<Student> table) {
        table.getItems().addAll(
                new Student("Dragon", "Smaug", "Dragon-icon.png"),
                new Student("Snake-eyes", "Shifty", "Medusa-icon.png"),
                new Student("Wood", "Solid", "Treant-icon.png"),
                new Student("Rainbow", "Magical", "Unicorn-icon.png")
        );
    }
}

class ImageCache {
    private static final int IMAGE_CACHE_SIZE = 10;
    private static final int THUMBNAIL_SIZE = 64;

    private static final ImageCache instance = new ImageCache();

    public static ImageCache getInstance() {
        return instance;
    }

    private final Map<String, Image> imageCache = new LruCache<>(
            IMAGE_CACHE_SIZE
    );

    private final Map<String, Image> thumbnailCache = new LruCache<>(
            IMAGE_CACHE_SIZE
    );

    public Image get(String url) {
        if (!imageCache.containsKey(url)) {
            imageCache.put(
                    url,
                    new Image(
                            Objects.requireNonNull(
                                    ImageCache.class.getResource(
                                            url
                                    )
                            ).toExternalForm(),
                            true
                    )
            );
        }

        return imageCache.get(url);
    }

    public Image getThumbnail(String url) {
        if (!thumbnailCache.containsKey(url)) {
            thumbnailCache.put(
                    url,
                    new Image(
                            Objects.requireNonNull(
                                    ImageCache.class.getResource(
                                            url
                                    )
                            ).toExternalForm(),
                            THUMBNAIL_SIZE,
                            THUMBNAIL_SIZE,
                            true,
                            true,
                            true
                    )
            );
        }

        return thumbnailCache.get(url);
    }

    private static final class LruCache<A, B> extends LinkedHashMap<A, B> {
        private final int maxEntries;

        public LruCache(final int maxEntries) {
            super(maxEntries + 1, 1.0f, true);
            this.maxEntries = maxEntries;
        }

        @Override
        protected boolean removeEldestEntry(final Map.Entry<A, B> eldest) {
            return super.size() > maxEntries;
        }
    }
}
 类似资料:
  • 我试图找到一种方法,将图像添加到JavaFx TableView列中,该列包含通过hibernate从H2数据库填充的其他列中的数据。TableView是在JavaFx场景生成器中设计的。 到目前为止,我一直在努力总结: 控制器类: 我得到一个错误在说. 这是我第一次尝试将图像添加到TableView中。 我从昨天开始四处看看,但现在似乎被卡住了。我希望得到一些帮助。我非常感谢你的帮助。 这是创建

  • 问题内容: 我正在尝试找到一种将图像添加到JavaFx TableView列的方法,该图像具有通过hibernate从H2数据库填充的其他列中的数据。TableView是在JavaFx Scene Builder中设计的。 到目前为止,这是我设法做到的: 控制器类: 我说那是一个错误。 这是我第一次尝试将图像添加到TableView中。 从昨天开始,我一直四处张望,但现在似乎被困住了。我希望能有所

  • 问题内容: 此处已触及该主题,但未提供有关如何创建3D图并在平面中以指定高度插入图像的指示。 因此,要提出一个简单且可复制的案例,假设我使用以下代码创建了一个3D图: 在视觉上,我们有: 在级别上,这里是避免重叠的视觉偏移, 我想插入一张图像, 表示曲线显示特定值的元素。 怎么做? 在此示例中,我并不关心元素与其值之间的完美匹配,因此请随时上传您喜欢的任何图像。另外,如果对匹配不满意,有没有办法让

  • 问题内容: 有没有一种方法(或编辑器)可以让我从源文件(例如* .java)中链接图像/文件/ http链接,有点像富文本文档? 这样,在阅读代码时,我可以快速查看附加的图像(增强注释),而不用打开浏览器等。 问题答案: Javadocs是HTML,因此您可以在其中嵌入图片: Eclipse会很高兴在javadoc视图中或将鼠标悬停在注释上时向您显示图像。其他IDE可能会或可能不会这样做。 显然,

  • 问题内容: 最近8个小时我一直在阅读文档,但没有发现任何可以帮助我的东西。大概是,但是没有代码在工作,因为它一直说“找不到图像URL”并引发异常。但是我还有其他项目,从来没有这个问题。 因此,有一个类包含这样的月份: 到目前为止,一切都很好。我什至可以在控制台中对其进行测试,并且效果很好,并且可以按值排序。现在,当我尝试从资源中添加图像时,出现了我之前提到的问题:找不到URL。但是,我只能使用图像

  • 我目前正在开发一个C#WPF应用程序,我试图在其中添加一个图像,然后在每个列表项中添加一些文本。 我已经为文本工作的绑定,但图像不显示。 下面是我的XAML: 下面是我的Directory清单类 下面是我如何添加项目到列表框 文字添加得很好,但图像没有。 我不确定这是否相关,但我在VS2010的控制台输出中看到以下内容 系统。Windows.Data错误:4:找不到引用为“RelativeSour