将Jtextfield字符串数据写入arraylist,并将相同的数据从arraylist读取到Jtextfields

时间:2014-06-25 17:04:28

标签: java arraylist

我一直在网上搜索我的问题的答案。我找到了一些很好的建议并改变了原来的代码,但我还没有找到我最初问题的答案。

我正在尝试从一系列Jtextfields中获取字符串数据并将它们写入arraylist,然后从arraylist中获取写入的数据并将其传输到相同的文本字段。

public class Form extends javax.swing.JFrame {


  public ArrayList<Personal> personalList;
  public ArrayList<Business> businessList;
  public ArrayList<Personal> displayPersonalList;
  public ArrayList<Business> displayBusinessList;      


public Form() {

    initArrayLists();

}

private void initArrayLists(){
    personalList = new ArrayList<Personal>();
    businessList = new ArrayList<Business>();
    displayPersonalList = new ArrayList<Personal>();
    displayBusinessList = new ArrayList<Business>();
}

这是我写入arraylists的提交按钮。

private void submitButtonActionPerformed(java.awt.event.ActionEvent evt)
{                                             
    if (contactTypeLabel.getText().equals("Personal Contact")){
        Personal p = new Personal();
        p.first = firstNameTF.getText();
        p.last = lastNameTF.getText();
        p.address = addressTF.getText();
        p.s = stateTF.getText();
        p.zip = zipTF.getText();
        p.phone = phoneTF.getText();
        p.email = emailTF.getText();
        personalList.add(p);

        Personal d = new Personal();
        d.first = firstNameTF.getText();
        displayPersonalList.add(p);

        resetTextFields();

    }else if (contactTypeLabel.getText().equals("Business Contact")){
        Business b = new Business();
        b.first = firstNameTF.getText();
        b.last = lastNameTF.getText();
        b.address = addressTF.getText();
        b.s = stateTF.getText();
        b.zip = zipTF.getText();
        b.phone = phoneTF.getText();
        b.email = emailTF.getText();
        businessList.add(b);

        Business d = new Business();
        d.first = firstNameTF.getText();
        displayBusinessList.add(d);

        resetTextFields();

    }

}

这里是更改为显示模式的代码,其中包含一个组合框,应该填充以访问arraylist以使用所选数据填充文本字段

private void displayPersonalButtonActionPerformed(java.awt.event.ActionEvent evt) 
{                                                      
    personalFieldViewer();
    submitButton.setVisible(false);
    displayComboBox.setVisible(true);
    clearTextFields();

    for (Object item : displayPersonalList) {
    displayComboBox.addItem(item);
    }

}

这是组合框操作的代码和填充文本字段的代码

private void displayComboBoxActionPerformed(java.awt.event.ActionEvent evt)
{                                                
    int x;
    switch (displayComboBox.getSelectedIndex()){
        case 0: 
            for (x = 0; x < x + 8; x ++) {
            switch (x){
                case 0 :firstNameTF.setText(personalList.get(x).toString());
                    break;
                case 1 :lastNameTF.setText(personalList.get(x).toString());
                    break;
                case 2 :addressTF.setText(personalList.get(x).toString());
                    break;
                case 3 :stateTF.setText(personalList.get(x).toString());
                    break;
                case 4 :zipTF.setText(personalList.get(x).toString());
                    break;
                case 5 :phoneTF.setText(personalList.get(x).toString());
                    break;
                case 6 :dobTF.setText(personalList.get(x).toString());
                    break;
                case 7 :emailTF.setText(personalList.get(x).toString());
                    break;
                    }
            }
            break;
        case 1: 
            for (x = 8; x < x + 8; x ++) {
            switch (x){
                case 8 :firstNameTF.setText(personalList.get(x).toString());
                    break;
                case 9 :lastNameTF.setText(personalList.get(x).toString());
                    break;
                case 10 :addressTF.setText(personalList.get(x).toString());
                    break;
                case 11 :stateTF.setText(personalList.get(x).toString());
                    break;
                case 12 :zipTF.setText(personalList.get(x).toString());
                    break;
                case 13 :phoneTF.setText(personalList.get(x).toString());
                    break;
                case 14 :dobTF.setText(personalList.get(x).toString());
                    break;
                case 15 :emailTF.setText(personalList.get(x).toString());
                    break;
                    }
            }
            break;
        case 2: 
            for (x = 16; x < x + 8; x ++) {
            switch (x){
                case 16 :firstNameTF.setText(personalList.get(x).toString());
                    break;
                case 17 :lastNameTF.setText(personalList.get(x).toString());
                    break;
                case 18 :addressTF.setText(personalList.get(x).toString());
                    break;
                case 19 :stateTF.setText(personalList.get(x).toString());
                    break;
                case 20 :zipTF.setText(personalList.get(x).toString());
                    break;
                case 21 :phoneTF.setText(personalList.get(x).toString());
                    break;
                case 22 :dobTF.setText(personalList.get(x).toString());
                    break;
                case 23 :emailTF.setText(personalList.get(x).toString());
                    break;
                    }
            }
            break;            


    }
}

这里是包含arraylists变量的类。

public class Contacts {
    public String first, last, address, s, zip, phone, email;

}

public class Personal extends Contacts{
   public String dob; 

}

public class Business extends Contacts{
    public String title, organization;

}

当我交替尝试使用* .add(textfield.getText())填充arraylists时; Java不允许使用变量first = firstNameTF.getText();然后* .add(第一个);我得到了同样的错误消息......

请尽量避免否定,我已阅读有关arraylists的API。谢谢。

1 个答案:

答案 0 :(得分:0)

您的arraylist声明的类型为Personal或Business。所以这些列表不能添加字符串值。所以当你说personalList.add(textfield.getText());它实际上试图将String对象添加到只能包含Personal对象的列表时。

其次,displayComboBoxActionPerformed()方法中的for循环导致无限循环。 for (x = 0; x < x + 8; x ++)。如果有不同的for循环和switch语句,你可以做类似

的事情
private void displayComboBoxActionPerformed(java.awt.event.ActionEvent evt) { 
    if(displayComboBox.getSelectedIndex() > -1){
        int start = displayComboBox.getSelectedIndex() * 8;
        for (int x = start; x < start + 8; x ++) {
            firstNameTF.setText(personalList.get(x).toString());
        }
    }
}