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

标签 java netbeans arraylist jcombobox

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

当前有一个名为 SundayList 的 ArrayList,一旦加载框架 AddStudent(GUI 位),就会加载它

添加学生类(class): 已编辑

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()])));

最佳答案

变量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.

关于java - 将方法中创建的 ArrayList 添加到 JCombobox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15345916/

相关文章:

java - maven项目无法启动tomcat

java - 我应该将 "mysql-connector-java-5.1.31-bin.jar"文件放在 Netbeans 中的哪个位置?

java - 我需要做什么才能让 LiveConnect 再次工作?

java - 调用已存储在 ArrayList 中的对象的方法

java - 遍历 ArrayLists 的问题

Java-使用 JSoup 抓取动态网站

java - 无法开始广播 : kurento. 找不到 MediaPipeline(代码 :40101, 类型 :null, 数据:{"type":"MEDIA_OBJECT_NOT_FOUND"})

java - spring boot 2 embed tomcat 9.0.26 无法加载jks文件流关闭

java - JSP:多个查询和生成键的问题

java - 从 ArrayList 中删除整数 IndexOutOfBoundsException