1

So I've been practising with JavaFX for a while now and while I love it, I find that my application is growing in size- especially in terms of the number of lines of codes for my FXML Controllers.

Right now a typical package for each scene in my application looks like this:

MyFXML.fxml

MyFXMLController.java

MyDataModel.java

For example, I have a form that takes in some information from textfields, comboboxes and radio buttons. When a button is pressed the form information is saved to a database and is also updated immediately in the tableview.

The tableview also allows deleting and updating of the information displayed.

With only a few components (approx. 10) I have a Controller class file that is about 550 lines long with about a 100 of it taken up by the injections (@FXML) and imports and growing!

My application would ideally have multiple of these different kinds of forms and a controller for each. The database queries are all different so it's not quote possible to abstract them out yet. Also, event listeners for the tableview generally require longer code in javafx than other components.

I just feel like there's a better approach to GUI building in javafx than what I'm doing and was wondering if there was some kind of reference I could look up?

I've read up on other stackoverflow answers on the Single Responsibility Principle. If the code below is any hint, my application simply creates a new staff member and allows updating the information. So I'm confused as to whether my class is too file or is this normal for GUI programming?

I'm not asking for coding help, I'm looking for recommendations on how I can improve GUI programming in javafx.

Thank you!

--

FWIW, here's what a sample controller file looks like

package myApp.staff;

//30 something lines of imports...

public class NewStaffMemberController implements Initializable {

    //80 something lines of private variables and @FXML injections

    public void setConn(Connection aConn) {
        conn = aConn;

        wrapGenderRadioButtons();

        populateDates();

        populateStaffTypeComboBox();

        populateDepartmentComboBox();

        populateStaffTable();
    }

    private void wrapGenderRadioButtons() {
        //4 lines
    }

    private void populateDates() {
        //25 lines
    }

    private void populateStaffTypeComboBox() {
        //20 lines
    }

    private void populateDepartmentComboBox() {
        //22 lines
    }

    private void populateStaffTable(){
        //longest at 100 lines. This code also adds the event listener for the tableview- makes it quite long!
    }

    private void editSelectedTableRow(Staff selectedstaff){
        //4 lines
    }

    @FXML
    private void selectedRadioBtnAction() {
        //1 lines
    }

    @FXML
    private void handleYearComboBoxAction() {
        //1 lines
    }

    @FXML
    private void handleMonthComboBoxAction() {
        //1 lines
    }

    @FXML
    private void handleDayComboBoxAction() {
        //1 lines
    }

    @FXML
    private void staffTypeComboBoxAction() {
        //1 lines
    }

    @FXML
    private void departmentComboBoxAction() {
        //1 lines
    }

    @FXML
    private void btnGenerateStaffId() {
        //36 lines
    }

    @FXML
    private void btnSaveInformation(){
        13 lines
    }

    private Boolean validateData() {

        //43 lines
    }

    private void assignStaffId() {
        //12 lines

    }

    private void insertIntoDatabase() {
        //35 lines
    }

    private void updateDatabase(){
       //35 lines
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
    }
}
Nepze Tyson
  • 587
  • 2
  • 7
  • 16

1 Answers1

2

Well, I think you don't have choice to inject your fxml fields you need. But maybe if you have number of injections like that just in one controller, you should maybe do a better conception of your app, by doing sub controllers working each other together and don't have everything in just one controller. One view doesn't mean one controller. You can have one view and multiple controller with there own view

agonist_
  • 4,890
  • 6
  • 32
  • 55
  • forgot to mark as correct. this actually helped me. Now I'm just having issues with passing data to the main controller from the nested one. – Nepze Tyson Jul 05 '13 at 03:10