0

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()])));
Geuni
  • 93
  • 1
  • 2
  • 12

1 Answers1

3

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.

Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • would that mean that I would have to create a class for every single list e.g. SundayList, MondayList, Tuesday...etc? – Geuni Mar 11 '13 at 18:38
  • I've changed according to the example above, except the coding "comboboxSunday.setModel(new javax.swing.DefaultComboBoxModel<>(sundayList.toArray(new String[sundayList.size()])));" where javx.swing had to be added due to Netbeans. However, there still is an error, and the frame(with the combobox) won't show up. I think it has to do with the adding to the combo box code? – Geuni Mar 11 '13 at 19:08
  • Still cant see where the frame is created. Consider posting the _full_ file – Reimeus Mar 11 '13 at 19:55
  • the first line of the edited version, "public class AddStudent extends javax.swing.JFrame" is where the frame is implemented, then created at the main method near the end of the coding. Am I placing the code in the wrong area? – Geuni Mar 11 '13 at 20:03
  • I'm sorry for constantly bothering you with this, but where should I add the calling 'loadList' code? – Geuni Mar 11 '13 at 20:14
  • I have the feeling placing code for falling `loadLists` is the final step, but I just can't figure it out... – Geuni Mar 11 '13 at 20:59
  • Do you only `sundayList` for loading the data? If so, you could use the `DefaultComboBoxModel` directly and use `addElement`. Also you should move the `setModel` calls to the `loadLists`. – Reimeus Mar 11 '13 at 21:05
  • isn't `addElement` only for Vectors? and the thing about moving `setModel` into `loadLists` is that once the program reaches the initalisation of the `comboboxsunday` the fields are initalised to become blank...so `setModel` should stay where it is? – Geuni Mar 11 '13 at 21:29
  • `DefaultComboBoxModel` has a method called `addElement`. I would move `setModel` to `addElement`. fields, do you mean `JComboBox` fields? The initial selected/displayed values can always be set in `loadLists` – Reimeus Mar 11 '13 at 21:36
  • aah alright. Btw where should the `AddStudent addStudent = new AddStudent(); addStudent.loadLists(); addStudent.setVisible(true);` be placed?? I feel that the wrong placement of this is causing my problems as well – Geuni Mar 11 '13 at 21:53
  • It's fine where it is. You will have to use `try/catch` block in your `run` method. See [this post](http://stackoverflow.com/questions/1369204/how-to-throw-a-checked-exception-from-a-java-thread) – Reimeus Mar 11 '13 at 22:03
  • Wow after hours of being stuck with this problem, I finally solved it! :) Thank you so much for answering everything Reimeus! – Geuni Mar 11 '13 at 22:20
  • oh god...Reimeus, before you go! I'm soo sorry again... When ran the frame on its own, the comboboxes display the correct items. But when I change from another frame and back to this frame `e.g. (via button actions) ThisFrame -> another frame -> ThisFrame ` the comboboxes don't have the array in them; they're empty. – Geuni Mar 11 '13 at 22:38