Nepze Tyson
Nepze Tyson

Reputation: 597

Using scene builder and multiple controllers

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!

Upvotes: 4

Views: 7501

Answers (1)

agonist_
agonist_

Reputation: 5032

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

Upvotes: 5

Related Questions