将方法中创建的ArrayList添加到JCombobox

时间:2013-03-11 18:31:51

标签: java netbeans arraylist jcombobox

之前我曾问过类似的问题,但意识到我无法解决的主要问题:

目前有一个名为 SundayList 的ArrayList,只要加载了 AddStudent 框架(GUI的位)就会加载

添加学生课程: 的 被修改

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);
            }
        });
    }

首先,我尝试将列表 SundayList 调用到组合框 comboboxSunday 中以填充它,但只是让找不到符号< / em>错误。

我需要做些什么来实现这一目标?

另外,我打算避免使用我之前见过的mySQL参与方法,因为我对它不熟悉..

组合框的当前编码

Netbeans为组合框自动生成的代码是:

comboboxSunday = new javax.swing.JComboBox();

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

1 个答案:

答案 0 :(得分:3)

变量SundayList仅限于构造函数的范围。假设您在JComboBox方法中创建initComponents,则无法访问此变量。

可以然后使SundayList成为一个类成员变量,允许你使用变量accross方法。最好还有一种方法来加载数据,而不是在UI构造函数中使用非UI功能:

public class AddStudent {
   private List<String> sundayList;
   private List<String> mondayList;
   ...

   private void loadLists() throws IOException {
      sundayList = new ArrayList<>();
      ...

然后添加:

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

别忘了给你的新加载方法打电话:

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

除了:请注意,Java命名约定表明变量以小写字母开头,这将使SundayList sundayList