1

Im attempting to get an array list contents in my controller passed through to my view to be displayed in the comboBox.

My current attempt consists of this method but when it runs nothing is shown in the combobox when im sure the arraylist has objects inside.

In the controller i have: where employees is the array list

public void setComboBox(){
    view.setComboBox(employees);
}

in the view i have: where jPatientComboList is the combo box

public void setComboBox(ArrayList<Employee> employees) {
    jEmployeeComboList.addItem(employees.get(employees.size()-1).getName());
}

I would like the combo box to display all names of the employees from the array list.

Ross
  • 141
  • 7
  • *"I would like the combo box to display all names of the employees from the array list"* - Use a look to add all the elements from the `ArrayList` to the combobox model – MadProgrammer Jan 15 '19 at 21:57
  • 1
    Possible duplicate [How do I populate a JComboBox with an ArrayList?](https://stackoverflow.com/questions/1291704/how-do-i-populate-a-jcombobox-with-an-arraylist) - if that doesn't solve your issue, consider providing a [mcve] which clearly demonstrates your issue – MadProgrammer Jan 15 '19 at 21:58

1 Answers1

0

In below line in your code, you are only adding the last item of the array list to the combo box.

jEmployeeComboList.addItem(employees.get(employees.size()-1).getName());

You can add all items like this:

for (Employee emp : employees) {
    jEmployeeComboList.addItem(emp.getName());
}
Prasad Karunagoda
  • 2,048
  • 2
  • 12
  • 16