将数据从后端的ArrayList传递到JComboBox GUI前端 - Java Swing

时间:2014-06-08 00:09:16

标签: java swing arraylist

这是我的FileIOManagement类,我想处理从文本文件中读取的所有内容,这些文件抓取数据以显示在GUI中。

这是我当前的FileIOManagement类的代码:

包裹摆动;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Scanner;

public class FileIOManagement {

     private ArrayList<String> nameList = new ArrayList<String>(); 
     private ArrayList<String> courseList = new ArrayList<String>(); 
     private ArrayList<String> semesterList = new ArrayList<String>();
     private ArrayList<String> moderatorList = new ArrayList<String>();
     private ArrayList<String> programList = new ArrayList<String>();
     private ArrayList<String> majorList = new ArrayList<String>();


     public FileIOManagement(){
         readTextFile();
     }

      private void readTextFile(){
         try{
             Scanner scan = new Scanner(new File("Course.txt"));

             while(scan.hasNextLine()){
             String line = scan.nextLine();            
             String[] tokens = line.split("~");
             String course = tokens[0].trim();
             String examiner = tokens[1].trim();
             String moderator = tokens[2].trim();
             String semester = tokens[3].trim();
             String program = tokens[4].trim();
             String major = tokens[5].trim();


             courseList.add(course);
             semesterList.add(semester);
             nameList.add(examiner);
             moderatorList.add(moderator);
             programList.add(program);
             majorList.add(major);
             HashSet hs = new HashSet();
             hs.addAll(nameList);
             nameList.clear();
             nameList.addAll(hs);
             Collections.sort(nameList);

         }
             scan.close();
         }
         catch (FileNotFoundException e){
             e.printStackTrace();
         }
     }



}

这是我需要传递给ArrayList数据的类。正如您所看到的,我正在尝试使用我试图通过ArrayList获取的数据填充comboBox1和comboBox2:

package swinging;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.border.EmptyBorder;


public class ReportGUI extends JFrame{
    //Fields
    private JButton viewAllReports = new JButton("View All Program Details");
    private JButton viewPrograms = new JButton("View Programs and Majors Associated with this course"); 
    private JButton viewTaughtCourses = new JButton("View Courses this Examiner Teaches"); 
    private JLabel courseLabel = new JLabel("Select a Course: ");
    private JLabel examinerLabel = new JLabel("Select an Examiner: "); 
    private JPanel panel = new JPanel(new GridLayout(6,2,4,4));  
    FileIOManagement fileName;
    ArrayList<String> names = new ArrayList<String>(fileName.getNameList());
    ArrayList<String> courses = new ArrayList<String>(fileName.getCourseList());








     public ReportGUI(){   
            reportInterface();

            allReportsBtn();     
//            fileRead();

            comboBoxes();
     }        




    private void reportInterface(){         
          setTitle("Choose Report Specifications");                   
          setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
          JPanel panel = new JPanel(new FlowLayout());        
          add(panel, BorderLayout.CENTER);
          setSize(650,200);
          setVisible(true);    
          setResizable(false);
          setLocationRelativeTo(null);
}    
    private void allReportsBtn(){
        JPanel panel = new JPanel(new GridLayout(1,1)); 
        panel.setBorder(new EmptyBorder(70, 50, 70, 25));
        panel.add(viewAllReports);        
        viewAllReports.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e){               
                new AllDataGUI();
            }
        });         
        add(panel, BorderLayout.LINE_END);
    }       
   private void comboBoxes(){     

        panel.setBorder(new EmptyBorder(0, 5, 5, 10));
       String[] comboBox1Array = names.toArray (new String[names.size()]);
        JComboBox comboBox1 = new JComboBox(comboBox1Array);          
        panel.add(examinerLabel);
        panel.add(comboBox1);          
        panel.add(viewTaughtCourses);
         viewTaughtCourses.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {                 
                 new ViewCourseGUI();
            }
        });  
         String[] comboBox2Array = courses.toArray(new String[courses.size()]);
         JComboBox comboBox2 = new JComboBox(comboBox2Array);
         panel.add(courseLabel);         
         panel.add(comboBox2); 
         panel.add(viewPrograms);
         viewPrograms.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {                 
                 new ViewProgramGUI();             
            }
        });  
         add(panel, BorderLayout.LINE_START); 

    }   

}

但是当我尝试编译时,我得到一个NullPointerException,如下图所示: enter image description here

我在这里做错了什么?

2 个答案:

答案 0 :(得分:1)

您所做的是,您试图获取fileName.getNameList() fileName从未实例化的结果,因为它将返回null

问题:

FileIOManagement fileName; //was not instantiated
ArrayList<String> names = new ArrayList<String>(fileName.getNameList()); //fileName.getNameList() is null
ArrayList<String> courses = new ArrayList<String>(fileName.getCourseList()); //fileName.getCourseList() is null

<强>溶液

在获取列表之前实例化FileIOManagement fileName

答案 1 :(得分:0)

想出来。

public class ReportGUI extends JFrame{
    //Fields
    private JButton viewAllReports = new JButton("View All Program Details");
    private JButton viewPrograms = new JButton("View Programs and Majors Associated with this course"); 
    private JButton viewTaughtCourses = new JButton("View Courses this Examiner Teaches"); 
    private JLabel courseLabel = new JLabel("Select a Course: ");
    private JLabel examinerLabel = new JLabel("Select an Examiner: "); 
    private JPanel panel = new JPanel(new GridLayout(6,2,4,4));  
    private FileIOManagement fileManage = new FileIOManagement();
    private ArrayList<String> nameList = new ArrayList();
    private  ArrayList<String> courseList = new ArrayList();

   // filename = fileManage.nameList();


    //FileIOManagement fileName = new FileIOManagement(fileName.getNameList());



  public void getData(){

      nameList = fileManage.getNameList();
      courseList = fileManage.getCourseList();
  }




     public ReportGUI(){ 
            getData();
            reportInterface();

            allReportsBtn();     


            comboBoxes();
     }        





    private void reportInterface(){         
          setTitle("Choose Report Specifications");                   
          setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
          JPanel panel = new JPanel(new FlowLayout());        
          add(panel, BorderLayout.CENTER);
          setSize(650,200);
          setVisible(true);    
          setResizable(false);
          setLocationRelativeTo(null);
}    
    private void allReportsBtn(){
        JPanel panel = new JPanel(new GridLayout(1,1)); 
        panel.setBorder(new EmptyBorder(70, 50, 70, 25));
        panel.add(viewAllReports);        
        viewAllReports.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e){               
                new AllDataGUI();
            }
        });         
        add(panel, BorderLayout.LINE_END);
    }       
   private void comboBoxes(){     

        panel.setBorder(new EmptyBorder(0, 5, 5, 10));
       String[] comboBox1Array = nameList.toArray (new String[nameList.size()]);
        JComboBox comboBox1 = new JComboBox(comboBox1Array);          
        panel.add(examinerLabel);
        panel.add(comboBox1);          
        panel.add(viewTaughtCourses);
         viewTaughtCourses.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {                 
                 new ViewCourseGUI();
            }
        });  
         String[] comboBox2Array = courseList.toArray(new String[courseList.size()]);
         JComboBox comboBox2 = new JComboBox(comboBox2Array);
         panel.add(courseLabel);         
         panel.add(comboBox2); 
         panel.add(viewPrograms);
         viewPrograms.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {                 
                 new ViewProgramGUI();             
            }
        });  
         add(panel, BorderLayout.LINE_START); 

    }   

}