Issue
I would want to update individual nodes according to the user input but the method save
(which works when the button saveAction
is clicked) tends only to update the last node in the nodes
. What is it I'm missing as I'm unable to get this work on the individual nodes produced?
public class Assessment implements Initializable {
@FXML
private VBox putItems;
@FXML
private Button saveAction;
Node[] nodes = new Node[Users.getDetails.size()];
FXMLLoader loader = null;
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
addNode();
}
private void addNode() {
try {
int i = 0;
int x = 1;
for (Object students : Users.getDetails() ) {
loader = new FXMLLoader();
nodes[i] = loader.load(
Objects.requireNonNull(getClass().getResource("marks.fxml"))
.openStream());
putItems.getChildren().add(nodes[i]);
Details assessment = loader.getController();
assessment.set_Name((String) students);
assessment.set_Number(String.valueOf(x));
assessment.set_Practice();
assessment.set_Class_50();
assessment.set_Total_all();
i++;
x++;
}
} catch (IOException e) {
e.printStackTrace();
}
}
@FXML
void save(ActionEvent event) throws SQLException {
for (int i = 0; i < nodes.length; i++){
Details assessment = loader.getController();
assessment.set_Class_50();
assessment.set_Practice_50();
assessment.set_Total_all();
}
}
}
Thank You!
Solution
A quick solution to get the existing code to update all the nodes might be to store the loaded controllers and update them:
public class Assessment implements Initializable {
...
private List <Details> subControllers = new ArrayList<>();
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
addNode();
}
private void addNode() {
try {
for (Object students : Users.getDetails() ) {
loader = new FXMLLoader();
...
Details assessment = loader.getController();
subControllers.add(assessment); // store controllers
...
}
} catch (IOException e) {
e.printStackTrace();
}
}
@FXML
void save(ActionEvent event) throws SQLException {
for (Details assessment : subControllers){
assessment.set_Class_50();
assessment.set_Practice_50();
assessment.set_Total_all();
}
}
}
Answered By - Sonnenkind
Answer Checked By - Marilyn (JavaFixing Volunteer)