I am creating a JavaFXML application and I need to have these classes there: ListOfAllEvents, Events and Instructors. ListOfAllEvents has an ArrayList of Events, and each Event has an ArrayList of Instructors.
Now, clicking on buttons and using methods for creating instances I first create several Instructors and add them to the ArrayList CurrentInstructors, then I create an instance of Event, assign the list of instructors to it and then I would like to add my new Event to the ArrayList CurrentEvents.
My problem is, CurrentEvents does add only one Event, and when I call the method for creating new Event for second time, the second event replaces the first one, even though my CurrentInstructors work correctly.
Can you help me? I am a beginner at Java, so I'll be thankful for every piece of advice.
My code:
public class FXMLDocumentController implements Initializable {
(...)
private ArrayList<Instructor> CurrentInstructors = new ArrayList<Instructor>();
private ArrayList<Event> CurrentEvents = new ArrayList<Event>();
@FXML
private void AddInstructor_Click(ActionEvent event) {
TextInputDialog dialog = new TextInputDialog("");
Optional<String> result = dialog.showAndWait();
if (result.isPresent()){
Instructor newInstructor = new Instructor(result.get());
CurrentInstructors.add(newInstructor);
}
}
private void NewEvent () {
Event newEvent = new Event(EventName.getText());
//this is the problematic row:
CurrentEvents.add(newEvent);
newEvent.setInstructors(CurrentInstructors);
CurrentInstructors.clear();
}
(...)
}
Instructor class:
public class Instructor {
private String name;
public String getName() {
return name;
}
public Instructor(String name){
this.name = name;
}
}
Event Class:
public class Event {
private String name;
private ArrayList<Instructor> Instructors = new ArrayList<Instructor>();
public Event(String name){
this.name = name;
}
//getters, setters
}