Using scene builder and multiple controllers

With the extension of my application, I would like to create additional classes for processing sql queries, validating data, etc. The controller currently controls all of this.

However, I have no idea how to create different classes that can "talk" to the components of my controller class and my .fxml file.

I confirmed this: XMLFML 1 FXML file with several different controllers?

.. and tried to create a new class to populate the 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 caused by a NullPointerException in

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

Therefore, I am not sure what to do at this moment.

Please, help. Thanks!

+4
source share
1 answer

Easier to create multiple FXML and Controller. 1 FXML = 1Controller. And you can communicate between different controllers without any problems.

Look at this part of the Nested Controller.

+5
source

Source: https://habr.com/ru/post/1489198/


All Articles