Search code examples
javanetbeansarraylistjcombobox

Adding an ArrayList created in a method to JCombobox


I've asked a similar question before, but realised the main issue at hand which I cannot solve:

Currently have an ArrayList called SundayList which is loaded as soon as the frame AddStudent is loaded (bit of GUI)

The Add Student class: Edited

public class AddStudent extends javax.swing.JFrame {

    public AddStudent() {
    initComponents();
     }
private void loadLists() throws IOException
    {
        //Creating the array of Activities to put into the ComboBoxes
        File f = new File("Activities.dat");

        sundayList = new ArrayList<>();
        mondayList= new ArrayList<>();
        tuesdayList= new ArrayList<>();
        wednesdayList= new ArrayList<>();
        thursdayList= new ArrayList<>();


try{
    BufferedReader reader = new BufferedReader(new FileReader(f));

     while(reader.ready())
        {
            String CDay = reader.readLine();                               
            String CActivityName = reader.readLine();
            String CSupervisor = reader.readLine();
            String CLocation = reader.readLine();
            String CPaid = reader.readLine();
            String nothing = reader.readLine();

            if(CDay.equals("Sunday"))
            {
                sundayList.add(CActivityName);
            }
            else if(CDay.equals("Monday"))
            {
                mondayList.add(CActivityName);
            }
            else if(CDay.equals("Tuesday"))
            {
                tuesdayList.add(CActivityName);
            }
            else if(CDay.equals("Wednesday"))
            {
                wednesdayList.add(CActivityName);
            }
            else if(CDay.equals("Thursday"))
            {
                thursdayList.add(CActivityName);
            }                
    }
    reader.close();
}
catch (IOException ex) 
{
    Logger.getLogger(StartUpFrame.class.getName()).log(Level.SEVERE, null, ex);
} 
}
...
comboboxSunday = new javax.swing.JComboBox();
...
}



 public static void main(String args[]) {

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new AddStudent().setVisible(true);
            }
        });
    }

For a start,I've tried to call the list SundayList into the combo box comboboxSunday to populate it, but only got the cannot find symbol error.

What do I need to do to make this possible?

Also, I plan on avoiding the mySQL involved method I've seen before, as I'm not familiar with it..

Current Coding for Combo box

The code automatically generated for the combo box by Netbeans is:

comboboxSunday = new javax.swing.JComboBox();

comboboxSunday.setModel(new javax.swing.DefaultComboBoxModel<>(sundayList.toArray(new String[sundayList.size()])));

Solution

  • The variable SundayList is limited to the scope of your constructor. Assuming you are creating your JComboBox in your initComponents method, you will not be able to access this variable.

    You could however make SundayList a class member variable allowing you to use the variable accross methods. Also better to have a method to load data rather than having non-UI functionality in a UI constructor:

    public class AddStudent {
       private List<String> sundayList;
       private List<String> mondayList;
       ...
    
       private void loadLists() throws IOException {
          sundayList = new ArrayList<>();
          ...
    

    Then to add:

    comboboxSunday.setModel(new DefaultComboBoxModel<>(sundayList.toArray(new String[sundayList.size()])));
    

    Don't forget to call your new load method:

    AddStudent addStudent = new AddStudent();
    addStudent.loadLists();
    addStudent.setVisible(true);
    

    Aside: note that Java naming conventions indicate that variable start with a lowercase letter which would make SundayList sundayList.