Using scene builder and multiple controllers


Question

With my application growing, I'd like to create additional classes to handle the sql queries, the data validation, etc. Currently, the controller manages it all.

However, I have no idea how to go about creating different classes that can "talk" to the components in my controller class and with my .fxml file.

I attemped this: JavaFX 1 FXML File with Multiple Different Controllers?

..and tried creating a new class to populate a table as follows:

import java.sql.Connection;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;

public class DepartmentTable {

    private Connection conn;
    private FXMLLoader fxmlLoader;
    @FXML
    private TableView<Department> departmentTableView;// = new TableView<>(staffTypeList);
    @FXML
    private TableColumn<Department, Integer> departmentIdCol;
    @FXML
    private TableColumn<Department, String> departmentNameCol;
    private ObservableList<Department> departmentList;

    public DepartmentTable(Connection aconn, FXMLLoader loader) {
        this.conn = aconn;
        this.fxmlLoader=loader;
        loadController();
        populateDepartmentTable();
    }

    private void loadController(){
        fxmlLoader.setController(this);
    }

    private void populateDepartmentTable() {

        departmentList = new DepartmentData(conn).getDepartmentList();

        /*Populate the Table with StaffType objects*/
        departmentIdCol.setCellValueFactory(
                new PropertyValueFactory<Department, Integer>("departmentID"));
        departmentNameCol.setCellValueFactory(
                new PropertyValueFactory<Department, String>("departmentName"));

        departmentTableView.setItems(departmentList);
    }
}

However, I get an invocationTargetException which is caused by a NullPointerException at

/*Populate the Table with StaffType objects*/
        departmentIdCol.setCellValueFactory(

So I am not sure what to do at this point.

Please help. Thank you!

1
4
5/23/2017 12:23:06 PM

Accepted Answer

It's more simple to create multiple FXML and Controller. 1 FXML = 1Controller. And you can comunicate betweeen the different controller with no problems.

Look at this part Nested Controller

5
7/2/2013 7:00:23 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon