Ivan Delgado Ivan Delgado - 4 days ago 6
Java Question

Populate a JComboBox with an ArrayList from a different class

I'm currently stuck in this part of a class project... I need to create an ArrayList from a text file; text file has account numbers that need to be populated in a JComboBox. This is what I have so far... only the first account number populates missing the rest not sure what my mistake is

// AccountUtility class that reads file and creates ArrayList named test

public class AccountUtility {

ArrayList<String> test = new ArrayList<String>();
String[] number;
String columns[], accountNumber, customerName, openDate, balance;
int size;


public AccountUtility(){


BufferedReader in = null;
try{ // assume products.txt already exists
in = new BufferedReader(new FileReader("accounts.txt"));
String line = in.readLine();
while(line != null) {
columns = line.split("<>");
accountNumber = columns[0];
customerName = columns[1];
openDate = columns[2];
balance = columns[3];

line = in.readLine();
}
in.close();
}
catch(IOException ioe)
{
System.out.println(ioe);
}
}

public ArrayList <String> getAccountNumbers( ){

ArrayList <String> test = new ArrayList<String>();
test.add(accountNumber);

return test;




//class with JComboBox (GUI)

public class BankAccountApp extends javax.swing.JFrame {


public BankAccountApp() {

initComponents();
setLocationRelativeTo(null);

AccountUtility gc = new AccountUtility();

for( String numbers : gc.getAccountNumbers()){
accountNumberComboBox.addItem(numbers);
}
}

Answer

I would have thought that what you want is

while(line != null)  {
    columns = line.split("<>");
    accountNumber = columns[0];
    test.add(accountNumber);
    ....
}

and

public ArrayList getAccountNumbers( ){
   return test;
}
Comments