-1

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!

i Devote
  • 3
  • 4
  • 2
    I suggest that you provide a [mcve], stub out the database call to just return dummy test data, and provide the fxml, so your issue can be replicated via copy and paste with no change, addition or libraries beyond the JDK and JavaFX. Update code to follow [naming conventions](https://www.oracle.com/java/technologies/javase/codeconventions-namingconventions.html) and format it using your IDE's format option. – jewelsea Aug 05 '22 at 22:51
  • You loop the node array length in save, but in each iteration, you are using the same loader reference, which will be the reference to the last loader you created in the addNode loop, so in the save method, you are just calling the same methods on the same assessment object for each loop iteration. – jewelsea Aug 05 '22 at 22:55
  • So how then do I get the loader reference based on the position in the array? – i Devote Aug 05 '22 at 23:09
  • "So how then do I get the loader reference based on the position in the array?" -> You don't. The loader is just for loading, you don't need it after loading is complete. After loading the loader has some information in it (the controller), you can get that from the loader once the loading is complete, and place the controller in an array or list, then iterate that list. – jewelsea Aug 05 '22 at 23:21
  • That suggestion I provided will work, but it will be a bit strange, it is just something to allow you to quickly continue. Perhaps you should review [MVC](https://stackoverflow.com/questions/32342864/applying-mvc-with-javafx) (also see the Eden link from that post), as applying MVC is probably a better way to solve the problems you are trying to solve. How specifically to apply that to your application is beyond the scope of what I will answer here. – jewelsea Aug 05 '22 at 23:22
  • Okay please, I'm checking it out... Thanks – i Devote Aug 05 '22 at 23:33

1 Answers1

-1

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();
    }
}
}
Sonnenkind
  • 74
  • 2
  • That is similar to what I did last two days, and it's working. I don't know the disadvantages yet. Hopefully it is safe. – i Devote Aug 20 '22 at 06:53