【问题标题】:JavaFX: Populating TableView with models from different classesJavaFX:使用来自不同类的模型填充 TableView
【发布时间】:2019-06-02 15:45:24
【问题描述】:

我知道有 20 个关于此的主题,但没有什么对我真正有用。

我有 2 个模型,我想从中填充 tableView。

一个是Student,有姓氏、名字和其他东西。

第二个叫做Termin(可能是英文的日期)。它有一个称为 Awlist 的二维列表,我们在其中存储学生在特定日期迟到的时间。我们这样做是因为我们使用的是 ORMlite,它与其他东西并没有什么不同。

我想要 4 列学生和 1 列,让我知道学生当天迟到的时间。但是我真的不能和我的团队一起解决它。

现在怎么样了:

idColumn.setCellValueFactory(new PropertyValueFactory<Student, String>("id"));
vornameColumn.setCellValueFactory(new PropertyValueFactory<Student, String>("vn"));
nachnameColumn.setCellValueFactory(new PropertyValueFactory<Student, String>("nn"));
matrikelnummerColumn.setCellValueFactory(new PropertyValueFactory<Student, String>("matnr"));
gruppeColumn.setCellValueFactory(cellData -> {
    if (cellData.getValue() == null)
        return new SimpleStringProperty("");
    else
        return new SimpleStringProperty(cellData.getValue().getGroup().getBezeichnung());
});
fehlzeitColumn.setCellValueFactory(new PropertyValueFactory<Student, String>("fehlZeit"));

tableView.setItems(getTableViewList());</code>

这个叫做“fehlZeit”的东西是学生来不及的时间。

我没有显示调用它的所有 List 方法。正确实施只是一个问题。我知道它应该是这样的,然后 getColumns().addAll 而不是 setItems() 对吗?

fehlzeitColumn.setCellValueFactory(new PropertyValueFactory<Student, String>("fehlZeit"));

【问题讨论】:

  • 不太确定你在问什么,但如果你想显示来自 2 个不同模型类的数据,你需要一个 TableView 的“包装器”类;包含您想要从两个模型中获得的字段的东西。

标签: javafx tableview


【解决方案1】:

您的问题不清楚如何从数据库中检索数据或为什么不能在查询中组合数据,因此我将演示如何使用多个对象模型来填充您的TableView

TableView 只能显示一种类型的项目,因此您不能简单地将不同的对象组合成一个 TableView。解决这个问题的方法是创建一个包装类来保存您想要从两个对象中获取的数据。

下面的示例将演示一种方法来执行此操作。共有三个类:StudentTimesDisplayStudentStudentTime 对象都来自您的数据库。

然后,我们根据匹配的StudentId 属性构建DisplayStudent 对象列表,结合StudentTimes

然后我们可以在TableView 中显示我们的DisplayStudent 对象列表。


import javafx.application.Application;
import javafx.beans.property.*;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class TableViewMultiModel extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {

        // Simple interface
        VBox root = new VBox(5);
        root.setPadding(new Insets(10));
        root.setAlignment(Pos.CENTER);

        // *** CREATE OUR SAMPLE DATA (these two lists would come from your database)
        ObservableList<Student> students = FXCollections.observableArrayList();
        ObservableList<Times> times = FXCollections.observableArrayList();

        // This is our list of DisplayStudents that combines all our data into one model for the TableView
        ObservableList<DisplayStudent> displayStudents = FXCollections.observableArrayList();

        // *** Now populate our lists (again, would be filled from your database
        students.addAll(
                new Student(1, "Jack"),
                new Student(2, "Breane")
        );
        times.addAll(
                new Times(1, "14:42"),
                new Times(2, "4:00"),
                new Times(1, "1:23"),
                new Times(1, "2:20"),
                new Times(2, "1:03")
        );

        // *** Now, we need to combine the items from the two lists into DisplayStudent objects which will be shown
        // *** in our TableView. Normally, you'd be doing this with SQL queries, but that depends on your database.
        for (Times time :
                times) {
            // For each Times, we want to retrieve the corresponding Student from the students list. We'll use the
            // Java 8 Streams API to do this
            students.stream()
                    // Check if the students list contains a Student with this ID
                    .filter(p -> p.getStudentId() == time.getStudentId())
                    .findFirst()
                    // Add the new DisplayStudent to the list
                    .ifPresent(s -> {
                        displayStudents.add(new DisplayStudent(
                                s,
                                time
                        ));
                    });
        }

        // *** Now that our model is in order, let's create our TableView
        TableView<DisplayStudent> tableView = new TableView<>();
        TableColumn<DisplayStudent, String> colName = new TableColumn<>("Name");
        TableColumn<DisplayStudent, String> colTime = new TableColumn<>("Late Minutes");

        colName.setCellValueFactory(f -> f.getValue().getStudent().nameProperty());
        colTime.setCellValueFactory(f -> f.getValue().getTimes().timeProperty());

        tableView.getColumns().addAll(colName, colTime);
        tableView.setItems(displayStudents);

        root.getChildren().add(tableView);

        // Show the Stage
        primaryStage.setWidth(300);
        primaryStage.setHeight(300);
        primaryStage.setScene(new Scene(root));
        primaryStage.show();
    }
}

class Student {

    private final IntegerProperty studentId = new SimpleIntegerProperty();
    private final StringProperty name = new SimpleStringProperty();

    public Student(int id, String name) {
        this.studentId.setValue(id);
        this.name.set(name);
    }

    public int getStudentId() {
        return studentId.get();
    }

    public IntegerProperty studentIdProperty() {
        return studentId;
    }

    public void setStudentId(int studentId) {
        this.studentId.set(studentId);
    }

    public String getName() {
        return name.get();
    }

    public StringProperty nameProperty() {
        return name;
    }

    public void setName(String name) {
        this.name.set(name);
    }
}

class Times {

    private int studentId;
    private final StringProperty time = new SimpleStringProperty();

    public Times(int studentId, String time) {
        this.studentId = studentId;
        this.time.set(time);
    }

    public int getStudentId() {
        return studentId;
    }

    public void setStudentId(int studentId) {
        this.studentId = studentId;
    }

    public String getTime() {
        return time.get();
    }

    public StringProperty timeProperty() {
        return time;
    }

    public void setTime(String time) {
        this.time.set(time);
    }
}

class DisplayStudent {
    private final ObjectProperty<Student> student = new SimpleObjectProperty<>();
    private final ObjectProperty<Times> times = new SimpleObjectProperty<>();

    public DisplayStudent(Student student, Times times) {
        this.student.set(student);
        this.times.set(times);
    }

    public Student getStudent() {
        return student.get();
    }

    public ObjectProperty<Student> studentProperty() {
        return student;
    }

    public void setStudent(Student student) {
        this.student.set(student);
    }

    public Times getTimes() {
        return times.get();
    }

    public ObjectProperty<Times> timesProperty() {
        return times;
    }

    public void setTimes(Times times) {
        this.times.set(times);
    }
}

结果:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-24
    • 2013-06-01
    • 2017-11-01
    • 2016-02-16
    • 2014-10-08
    • 2018-10-14
    • 2013-06-06
    相关资源
    最近更新 更多