使用JavaFX中的H2数据库从其他模型填充TableView

时间:2018-10-27 00:10:15

标签: java javafx h2 ormlite

我想用当前学期的名称填写一栏。它会被称为“ bezeichnung”。我没有得到显示的学期,而是使用@xyz知道了存储方向。问题是数据库具有例如表(以及在Java中具有getter和setter的模型)的分组和学期。在分组中,需要一个setSemester和getSemester,因为我想添加和删除columnItems,并且有必要选择一个学期。所以我应该通过ID获得学期,但是我不知道怎么做:

 //configure the table
@FXML private TableView<Groupage> tableView;
@FXML private TableColumn<Groupage, String> IDColumn;
@FXML private TableColumn<Groupage, String> BezeichnungColumn;
@FXML private TableColumn<Groupage, Semester> SemesterColumn;

DBManager connector = new DBManager();

public void buttonAddGroupage() {
    Groupage grou = new Groupage();

    grou.setBezeichnung(Bezeichnung.getText());
    grou.setSemester(Semesterauswahl.getValue());

    connector.addGroupage(grou);
    initialize(null, null);
}

public void buttonDeleteGroupage() {
    Groupage grou = tableView.getSelectionModel().getSelectedItem();    
    connector.deleteGroupage(grou, true);
    initialize(null, null);
}

public void buttonDetails() {

}

public void initialize(URL url, ResourceBundle rb) {
    //set up the columns in the table
    IDColumn.setCellValueFactory(new PropertyValueFactory<Groupage, String>("id"));
    BezeichnungColumn.setCellValueFactory(new PropertyValueFactory<Groupage, String>("bezeichnung"));
    SemesterColumn.setCellValueFactory(new PropertyValueFactory<Groupage, Semester>("semester"));

    //load dummy data
    tableView.setItems(getGroupage());

    //Update the table to allow for the first and last name fields
    //to be editable
    tableView.setEditable(true);
    BezeichnungColumn.setCellFactory(TextFieldTableCell.forTableColumn());
    //SemesterColumn.setCellFactory(TextFieldTableCell.forTableColumn());

    //This will allow the table to select multiple rows at once
    tableView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
   // List<Semester> semester = connector.showSemesterAll();

   // for (int i=0; i < semester.size(); i++) {
   //   semester.add(semester.get(i));
  //  }
    Semesterauswahl.setItems(getSemester());


    //Semesterauswahl.getItems().add(null);

}

 public ObservableList<Groupage> getGroupage() {
        ObservableList<Groupage> groupage = FXCollections.observableArrayList();

        ArrayList<Groupage> merge = (ArrayList<Groupage>) connector.showGroupageAll();

        for (int i=0; i < merge.size(); i++) {
            groupage.add(merge.get(i));
        }
        return groupage;
 }

 public ObservableList<Semester> getSemester() {
        ObservableList<Semester> semester = FXCollections.observableArrayList();

        ArrayList<Semester> merge = (ArrayList<Semester>) connector.showSemesterAll();

        for (int i=0; i < merge.size(); i++) {
            semester.add(merge.get(i));
        }
        return semester;
 }

H2中的数据库方法如下:

    public List<Semester> showSemesterAll() {
            try {
                ConnectionSource connectionSource = new 
    JdbcConnectionSource(DB_URL, USER, PASS);
                Dao<Semester, String> semDao = 
   DaoManager.createDao(connectionSource, Semester.class);
            List<Semester> reSemester = new ArrayList<>();
            CloseableIterator<Semester> iterator = semDao.closeableIterator();
            try {
                while (iterator.hasNext()) {
                    Semester mysemcycle = iterator.next();
                    reSemester.add(mysemcycle);
                }

            } finally {
                iterator.close();
            }

            return reSemester;
        } catch (SQLException | IOException se) {
            se.printStackTrace();
        }
        return null;
    }

0 个答案:

没有答案