我在将一些数据发送到另一个名为ValuesSubmitted.java的类文件时遇到了很大的麻烦,但是只能通过位于我的主Controller.java中的一种方法来发送数据。
我的问题是,在Controller.java类的所有其他区域中,我可以成功地将ValuesSubmitted.java类中的值设置为GET / GET。 ValuesSubmitted没有附加相应的FXML元素,因为ValuesSubmitted是用于数据源的。
请遵循我的代码,因为我的评论将进一步详细解释。
@FXML
private void ResetPersonnel(){
//Instantiating ValuesSubmitted java class
ValuesSubmitted values = new ValuesSubmitted();
try (Connection connection = DriverManager.getConnection(CONNECTION_STRING)) {
//Fetching SQL DB - working as intended
final String queryTitle = "SELECT * FROM PERSONNEL WHERE TRUCK_TYPE = '" + values.getTypeOfTruck() + "' AND TRUCK_NUMBER = '"
+ values.getTruckNumber() + "' AND SHIFT_COLOR = '" + values.getShiftColor()+"';";
PreparedStatement statementTitle = connection.prepareStatement(queryTitle);
String resultTitle;
ResultSet resultSetTitle = statementTitle.executeQuery();
while (resultSetTitle.next()) {
// Each result
resultTitle = resultSetTitle.getString("TITLE") + " " + resultSetTitle.getString("LAST_NAME");
// THIS ADDS EACH ENTRY FOUND INTO OBSERVABLE "list" variable located in this Controller.java class.(this works.)
list.add(resultTitle);
// This is SUPPOSE to add each entry found into an observable list called "personnel" located in a single class file called ValuesSubmitted.java
// ValuesSubmitted doesn't have a FXML page for it. It was intended for a data storage with all my setters and getters.
values.setPersonnel(list);
}
// When I Sys-Out this line, it returns 0 upon this method being called.
System.out.println(values.getPersonnel().size());
// This is my ListView instance object with "list" being the variable of my observable list.
// This populates my List view container successfully with the observable list located in Controller.java
crewMembersList.setItems(list);
/*
My problem is that in all other areas of my Controller.java class I can successfully SET and GET values in my ValuesSubmitted.java class with no issues.
Again, ValuesSubmitted not does have a corresponding FXML element attached to it, as ValuesSubmitted was intended for a data source.
*/
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
这是Controller.java中的另一种方法,可以成功将数据设置为ValuesSubmitted.java类。
@FXML
private void addCrewMember() {
//TODO FIX DUPLICATE ENTRIES WITH CASE SENSITIVITY
TextInputDialog addPersonnel = new TextInputDialog();
addPersonnel.setTitle("Personnel Names");
addPersonnel.setHeaderText("Enter personnel title then last name.");
addPersonnel.setContentText("ex. \"FF Smith\"");
Optional<String> result = addPersonnel.showAndWait();
if (result.isPresent() && !addPersonnel.getResult().isEmpty()) {
values.setPersonnelMembers(addPersonnel.getResult());
}
// This line validates that the entry above was indeed added to the list.
System.out.println(values.getPersonnel());
}
答案 0 :(得分:0)
list.add(resultTitle);
我想这是在ValuesSubmitted values
中分配人员列表的中间步骤,没关系。我建议先检查其大小或打印元素。
对于似乎有问题的setPersonnel(List list)
,请尝试将临时列表中的每个元素复制到ValuesSubmitted.personnelList
。您可以使用java.util.ArrayList.addAll(Collection<? extends E> c)
因此您需要类似values.getPersonnel().addAll(list);
答案 1 :(得分:0)
这些方法之间的区别在于,第二个方法values
是一个可能在其他用户交互期间填充的字段。
在第一种方法中,您创建一个存储在局部变量中的新鲜实例:
ValuesSubmitted values = new ValuesSubmitted();
此实例的属性可能未初始化。加载fxml时,您可能需要将实例传递给控制器,请参见Passing Parameters JavaFX FXML。
一件奇怪的事情是您在每次循环迭代中都传递了列表。您可能应该将此语句移出循环:
values.setPersonnel(list);
此外,由于list
似乎是一个可重用的字段,因此在检索数据之前可能有必要清除它。