0

我对 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>();
        }
4

1 回答 1

1

您的问题可能有点过于宽泛,无法在 StackOverflow 上回答,但是,我会给出一个链接到其他一些问题和场外资源的答案,无论如何这些问题可能对您有所帮助。


用于从 JavaFX 访问本地数据库的示例,使用并发任务进行数据库操作,以便 UI 保持响应。

该示例的相关 StackOverflow 问题是:

该示例基于嵌入式H2 数据库

此示例使用 JDBC,它适用于示例中演示的一些基本操作。对于更广泛的系统,最好使用 JPA 而不是直接使用 JDBC。

更多资源:

比将 JavaFX 应用程序直接连接到数据库更灵活的设计是使用三层架构而不是两层架构。为此,您将在数据库前面使用基于网络的服务层(例如,使用 JAX-RS 的 SpringBoot 或 WildFly Swarm),然后从客户端应用程序与服务层进行通信。服务层负责所有与数据库相关的操作。但是,构建这样的应用程序比直接连接到数据库要复杂得多。

您可能希望在应用程序中使用一些基本的 MVC 设计原则,而不是像您在问题中提供的那样将数据模型嵌入到控制器中。执行 MVC 部分可能对您来说有点棘手。你可以用谷歌搜索它并使用一个框架,但这最终可能会让你感到困惑。也许,只是尝试在没有任何框架的情况下对其进行编码,但也许只是一些定制设计工作,如果需要,可以改造一个 MVC 类型系统,如 Afterburner.fx 或 Gluon Ignite,如果你发现你会从这样的事情中受益你在发展中取得进展。

鉴于这是一个学生项目而不是大型商业产品,我建议只使用最简单的解决方案,扩展前面提到的基于 H2 的示例以提供您需要的功能,而不是引入许多其他技术或库,例如 JPA 、JAX-RS、网络服务等。

于 2016-05-17T21:45:02.957 回答