JavaFX:使用来自不同类的模型填充TableView

时间:2019-01-07 23:56:15

标签: javafx tableview

我知道有20个关于此的线程,但对我来说真的没有任何作用。

我有2个要填充tableView的模型。

一个是有姓,名和名字的学生。

第二个称为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"));

1 个答案:

答案 0 :(得分:0)

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

TableView只能显示一种类型的项目,因此您不能简单地将不同的对象组合到单个TableView中。解决此问题的方法是创建一个包装器类,以容纳两个对象中所需的数据。

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

然后,我们基于匹配的DisplayStudent属性,建立Student对象的列表,结合TimesStudentId

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


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);
    }
}

  

结果:

screenshot