【问题标题】:Adding an ArrayList created in a method to JCombobox将方法中创建的 ArrayList 添加到 JCombobox
【发布时间】:2013-03-11 18:31:50
【问题描述】:

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

当前有一个名为 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 以填充它,但只得到 找不到符号 错误。

我需要做些什么才能让这成为可能?

另外,我计划避免使用我以前见过的涉及 mySQL 的方法,因为我不熟悉它..

组合框的当前编码

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

comboboxSunday = new javax.swing.JComboBox();

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

【问题讨论】:

    标签: java netbeans arraylist jcombobox


    【解决方案1】:

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

    可以不过将SundayList 设为类成员变量,允许您在跨方法中使用该变量。最好有一种方法来加载数据,而不是在 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

    【讨论】:

    • 这是否意味着我必须为每个列表创建一个类,例如SundayList、MondayList、Tuesday...等?
    • 我已经按照上面的例子改了,除了编码 "comboboxSunday.setModel(new javax.swing.DefaultComboBoxModel(sundayList.toArray(new String[sundayList.size()])) );"由于Netbeans,必须添加javx.swing。但是,仍然存在错误,并且框架(带有组合框)不会出现。我认为这与添加到组合框代码有关?
    • 仍然看不到框架的创建位置。考虑发布完整文件
    • 编辑版本的第一行,“public class AddStudent extends javax.swing.JFrame”是实现框架的地方,然后在接近编码结束的main方法处创建。我是否将代码放在错误的区域?
    • 很抱歉一直困扰您,但我应该在哪里添加调用“loadList”代码?
    猜你喜欢
    • 1970-01-01
    • 2019-04-01
    • 2013-02-26
    • 1970-01-01
    • 1970-01-01
    • 2015-01-23
    • 1970-01-01
    • 2015-01-16
    相关资源
    最近更新 更多