Java从csv文件获取id到组合框

时间:2015-08-16 10:36:54

标签: java arrays combobox jcombobox

我有一个计划,有两个表格,一个是咨询顾问和顾客。在第一个表单上,用户将输入顾问详细信息,他的ID将保存在csv文件中,这样可以正常工作。

Consultant cons_save = new Consultant();
    cons_save.setPersonfirstname(this.jTextField1.getText());
    cons_save.setPersonlastname(this.jTextField2.getText());
    cons_save.setPersonID(this.jTextField4.getText());
    this.jTextField1.setText("");
    this.jTextField2.setText("");
    this.jTextField3.setText("");

    cons_save.ConsultantID = cons_save.PersonID;

    cons_save.setConsultantID(this.jTextField4.getText());
    this.jTextField4.setText("");

    try
{
        BufferedWriter writer = new BufferedWriter(new FileWriter("E:\\ryan_assignment_sit2\\ConsID\\consID.csv", true));

    writer.append(cons_save.ConsultantID);  
        writer.append(",");
    writer.flush();
    writer.close();
}
catch(IOException e)
{
     e.printStackTrace();
} 

    cons_save.savecons();  

保存id之后,Id从csv调用到数组,这也可以正常工作。

       public CreateCustomer() {
    initComponents();

    ArrayList<String> ConsIDList = new ArrayList<String>();

    String csvFileToRead = "E:\\ryan_assignment_sit2\\ConsID\\consID.csv"; // Reads the CSV File.
    BufferedReader br = null; // Creates a buffer reader.
    String line = "";
    String splitBy = ","; // Reader Delimiter

    try {
        br = new BufferedReader(new FileReader(csvFileToRead)); // Buffer Reader with file name to read.
        Scanner reader = new Scanner(System.in);
        while ((line = br.readLine()) != null) { //While there is a line to read.
            reader = new Scanner(line);
            reader.useDelimiter(splitBy);

            while (reader.hasNext()) { // While there is a next value (token).
                ConsIDList.add(reader.next());
            }
        }

    } catch (FileNotFoundException exception) { // Exception Handler if the File is not Found.
        exception.printStackTrace();
    } catch (IOException exception) { // Input/Output exception
        exception.printStackTrace();
    } finally {
        if (br != null) {
            try {
                br.close(); // Close the Scanner.
            } catch (IOException exception) {
                exception.printStackTrace();
            }
        }



        Vector<String> vectorData = new Vector<String>(ConsIDList);

        DefaultComboBoxModel<String> comboBoxModel = new DefaultComboBoxModel<>(vectorData);

        this.jComboBox1.setModel(comboBoxModel);



    }
}

阵列工作正常,但组合框没有填充arraylist。

0 个答案:

没有答案