我完全卡住了。我尝试用表视图创建stage,但只有在单击按钮后数据才需要存在。当我把所有代码放在initialize方法中时,一切都是正确的,tableview也实现了,但是当我尝试分离代码时(在initialize setcellvaluefactory中,但我方法点击按钮后填充可观察列表)我总不能这样做-方法正在工作我正在连接数据库(我看到hibernate输出),但在表视图上没有任何更改:(我没有任何错误。
package org.example;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.MapChangeListener;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.MapValueFactory;
import javafx.stage.Stage;
import org.example.DAO.CoursesInfoDAO;
import org.example.entity.CoursesInfo;
import java.net.URL;
import java.util.*;
public class StudentViewController {
@FXML
TableView<Map<String, Object>> studentTable;
@FXML
TableColumn<Map, String> language;
@FXML
TableColumn<Map, String> level;
@FXML
TableColumn<Map, String> surname;
@FXML
TableColumn<Map, Integer> price;
@FXML
TableColumn<Map, String> startDate;
@FXML
TableColumn<Map, String> endDate;
@FXML
TableColumn<Map, String> seats;
@FXML
TableColumn<Map, String> occupied;
@FXML
Button allCourses;
public TableView getStudentTable() {
return studentTable;
}
public StudentViewController() {
}
public ObservableList<Map<String, Object>> items;
@FXML
public void initialize(URL location, ResourceBundle resources) {
language.setCellValueFactory(new MapValueFactory("language"));
//level = new TableColumn<>("Poziom");
level.setCellValueFactory(new MapValueFactory("level"));
//surname = new TableColumn<>("Nazwisko nauczyciela");
surname.setCellValueFactory(new MapValueFactory("surname"));
//price = new TableColumn<>("Cena");
price.setCellValueFactory(new MapValueFactory("price"));
//startDate = new TableColumn<>("Początek");
startDate.setCellValueFactory(new MapValueFactory("startDate"));
//endDate = new TableColumn<>("Koniec");
endDate.setCellValueFactory(new MapValueFactory("endDate"));
//seats = new TableColumn<>("Miejsca");
seats.setCellValueFactory(new MapValueFactory("seats"));
//occupied = new TableColumn<>("Zajęte");
occupied.setCellValueFactory(new MapValueFactory("occupied"));
}
private Stage loginStage;
public void setLoginStage(Stage loginStage) {
this.loginStage = loginStage;
}
@FXML
private void displayAllcourses() {
items = FXCollections.observableArrayList();
//ObservableList<Map<String,Object>> temp = FXCollections.observableArrayList();
//studentTable.setItems(temp);
CoursesInfoDAO coursesInfoDAO = new CoursesInfoDAO();
List<CoursesInfo> allCourses = coursesInfoDAO.getListOfCourses();
for (CoursesInfo i : allCourses) {
Map<String, Object> item = new HashMap<>();
item.put("language", "ola");
item.put("level", "ola");
item.put("surname", "ola");
item.put("price", "ola");
item.put("startDate", "ola");
item.put("endDate", "ola");
item.put("seats", i.allSeats);
item.put("occupied", "ola");
//studentTable.getItems().add(item);
//studentTable.refresh();
items.add(item);
//temp.add(item);
}
studentTable.setItems(items);
}
}
暂无答案!
目前还没有任何答案,快来回答吧!