0

I'm pretty new to JavaFX, and also not incredibly familiar with using databases in Java in general, but I have to convert an existing JavaFX program I made to remove a list that is populating a TableView table and replace it with items from a database and giving the user the ability to add or delete as well. This is essentially a very simplified version of a course registration program for a student. Everything works as it is, I just need to use a database so that a faculty side can be used to view which students are registered for which classes. I'm just not familiar with the best way to call the database and apply it to the tableView. I want to be able to select a row on the table, click my action button and register that student. Any help is greatly appreciated.

Code:

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>();
        }
Hunter Noman
  • 1
  • 1
  • 7

1 Answers1

1

Your question is probably a bit too broad to be answered on StackOverflow, but well, I'll put an answer that links to some other questions and off-site resources that may help you anyway.


Sample for accessing a local database from JavaFX using concurrent tasks for database operations so that the UI remains responsive.

Related StackOverflow question for the sample is:

The sample is based upon an embedded H2 database.

This sample uses JDBC, which is fine for a few basic operations such as are demonstrated in the sample. For a more extensive system, it would be better to use JPA rather than JDBC directly.

Further resources:

A more flexible design than connecting your JavaFX application directly to a database, is to use a three tier architecture rather than a two tier architecture. To do this you would front the database with a network based service layer (e.g. SpringBoot or WildFly Swarm using JAX-RS) and then communicate from your client applications to the service layer. The service layer is responsible for all of the database related operations. However, constructing such an application is quite a bit more complex than directly connecting to the database.

You may want to use some basic MVC design principles in the application, rather than embedding the data model in your controller as you have provided in your question. Doing the MVC part might get a little tricky for you. You can google about it and use a framework, but that might end up confusing you. Perhaps, just try to code it up without any framework, but maybe just a little custom design work and, if needed, retrofit an MVC type system such as Afterburner.fx or Gluon Ignite, if you find you would benefit from such a thing as you progress in development.

Given this is a student project rather than a large commercial offering, I'd advise just going for the simplest solution, extending the H2 based sample referenced earlier to provide the functionality you need and not introducing a lot of other technologies or libraries such as JPA, JAX-RS, networked services, etc.

Community
  • 1
  • 1
jewelsea
  • 150,031
  • 14
  • 366
  • 406