随着我的应用程序的增长,我想创建额外的类来处理 sql 查询、数据验证等。目前,控制器管理这一切。
但是,我不知道如何创建不同的类,这些类可以与我的控制器类和我的 .fxml 文件中的组件“对话”。
我尝试了这个:JavaFX 1 FXML File with Multiple different Controllers?
..并尝试创建一个新类来填充表,如下所示:
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);
}
}
但是,我得到一个由 NullPointerException 引起的 invocationTargetException
/*Populate the Table with StaffType objects*/
departmentIdCol.setCellValueFactory(
所以我现在不知道该怎么做。
请帮忙。谢谢!