我对 JavaFX 还是很陌生,一般来说也不太熟悉在 Java 中使用数据库,但是我必须转换我制作的现有 JavaFX 程序以删除填充 TableView 表的列表并将其替换为数据库中的项目并赋予用户添加或删除的能力。这本质上是学生课程注册程序的一个非常简化的版本。一切正常,我只需要使用一个数据库,以便可以使用教师端查看哪些学生注册了哪些课程。我只是不熟悉调用数据库并将其应用于 tableView 的最佳方法。我希望能够选择表格上的一行,单击我的操作按钮并注册该学生。任何帮助是极大的赞赏。
代码:
public class Screen3Controller implements Initializable {
@FXML private Button continuebtn;
@FXML private Button studAddCrseBtn;
@FXML private Button yesBtn;
@FXML private Label regStatus;
@FXML private TableView<SummerClass> table;
@FXML private TableColumn<SummerClass, Integer> id;
@FXML private TableColumn<SummerClass, String> dept;
@FXML private TableColumn<SummerClass,Integer> number;
@FXML private TableColumn<SummerClass, String> title;
@FXML private TableColumn<SummerClass, String> day;
@FXML private TableColumn<SummerClass, String> time;
@FXML private TableColumn<SummerClass, Boolean> checkbox;
private ObservableSet<CheckBox> selectedCheckBoxes = FXCollections.observableSet();
private ObservableSet<CheckBox> unselectedCheckBoxes = FXCollections.observableSet();
private IntegerBinding numCheckBoxesSelected = Bindings.size(selectedCheckBoxes);
@FXML
private IntegerProperty index = new SimpleIntegerProperty();
private final int maxNumSelected = 3;
public ObservableList<SummerClass> list1 = FXCollections.observableArrayList(
new SummerClass (10001, "ACCT", 1010 , "Intro to Acct (3)", "MWF", "1:00 - 2:15" ),
new SummerClass (10002, "ACCT", 2010 , "Acct for Bus. (3)", "MWF", "9:00 - 10:15" ),
new SummerClass (10003, "ART", 1010 , "Fund. of Art (3)", "TR", "3:00 - 4:15" ),
new SummerClass (10004, "ART", 1110 , "Art History (3)", "MWF", "1:00 - 2:15" ),
new SummerClass (10005, "BIOL", 1010 , "Principles of Biology I (3)", "TR", "9:00 - 10:15" ),
new SummerClass (10006, "BIOL", 2010 , "Principles of Biology II (3)", "MWF", "3:00 - 4:15" ),
new SummerClass (10007, "CHEM", 1010 , "Principles of Chemistry I (3)", "MWF", "1:00 - 2:15" ),
new SummerClass (10008, "CHEM", 2010 , "Principles of Chemistry II (3)", "TR", "9:00 - 10:15" ),
new SummerClass (10009, "CPSC", 1010 , "Java I (3)", "TR", "3:00 - 4:15" ),
new SummerClass (10010, "CPSC", 2010 , "Java II (3)", "MWF", "9:00 - 10:15" ),
new SummerClass (10011, "ENGL", 1010 , "Composition I (3)", "MWF", "1:00 - 2:15" ),
new SummerClass (10012, "ENGL", 2010 , "Composition II (3)", "TR", "3:00 - 4:15" ),
new SummerClass (10013, "FIN", 1010 , "Principles of Finance I (3)", "MWF", "1:00 - 2:15" ),
new SummerClass (10014, "FIN", 2010 , "Principles of Finance II (3)", "TR", "9:00 - 10:15" ),
new SummerClass (10015, "GEO", 1010 , "Intro to Geology (3)", "MWF", "3:00 - 4:15" ),
new SummerClass (10016, "GEO", 3610 , "Geology in History (3)", "TR", "1:00 - 2:15" ),
new SummerClass (10017, "HIST", 1210 , "Western History (3)", "MWF", "9:00 - 10:15" ),
new SummerClass (10018, "HIST", 1510 , "Fund. of History (3)", "MWF", "3:00 - 4:15" ),
new SummerClass (10019, "MATH", 1010 , "Elementary Algebra (3)", "TR", "1:00 - 2:15" ),
new SummerClass (10020, "MATH", 2010 , "Linear Algebra (3)", "MWF", "9:00 - 10:15" ),
new SummerClass (10021, "NUTR", 1010 , "Nutrition Fundamentals (3)", "TR", "3:00 - 4:15" ),
new SummerClass (10022, "PHYS", 1010 , "Intro to Physics (3)", "MWF", "1:00 - 2:15" ),
new SummerClass (10023, "PHYS", 2010 , "Physics II (3)", "MWF", "9:00 - 10:15" ),
new SummerClass (10024, "POL", 1010 , "Political Science I (3)", "TR", "3:00 - 4:15" ),
new SummerClass (10025, "POL", 1010 , "Political Science II (3)", "MWF", "1:00 - 2:15" ),
new SummerClass (10026, "STEM", 1010 , "Stem Education I (3)", "TR", "9:00 - 10:15" ),
new SummerClass (10027, "STEM", 2010 , "Advanced Stem Education (3)", "TR", "3:00 - 4:15" ),
new SummerClass (10028, "STEM", 3010 , "Stem Education in Business (3)", "MWF", "1:00 - 2:15" ));
final ObservableList<SummerClass> list2 = FXCollections.observableArrayList();
@Override
public void initialize(URL location, ResourceBundle resources) {
id.setCellValueFactory(new PropertyValueFactory<>("id"));
dept.setCellValueFactory(new PropertyValueFactory<>("dept"));
number.setCellValueFactory(new PropertyValueFactory<>("number"));
title.setCellValueFactory(new PropertyValueFactory<>("title"));
day.setCellValueFactory(new PropertyValueFactory<>("day"));
time.setCellValueFactory(new PropertyValueFactory<>("time"));
checkbox.setCellValueFactory(new PropertyValueFactory<SummerClass, Boolean>(""));
checkbox.setCellFactory(new Callback<TableColumn<SummerClass, Boolean>, TableCell<SummerClass, Boolean>>(){
public TableCell<SummerClass, Boolean> call(TableColumn<SummerClass, Boolean> p){
return new CheckBoxTableCell<SummerClass, Boolean>();
}