I’m having some difficulty adding an arrayList of user details to another arrayList which contains details of each user i create.
I have 2 classes: addUser and Database.
In the addUser class i have the following code:
JLabel submitButton = new JLabel("Submit: ");
contentPane.add(submitButton);
JButton addUser = new JButton("+ Add User");
addUser.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
addToArray();
databaseArray.add(app);
frame.dispose();}});
contentPane.add(addUser);
frame.pack();
frame.setVisible(true);
frame.setLocationRelativeTo(null);
}
//arraylist of details for a single applicant
final ArrayList<String> app = new ArrayList<String>();
public void addToArray()
{
appNumber = appNumberField.getText();
name = nameField.getText();
date = dateField.getText();
fileLoc = fileLocField.getText();
country = countryRefField.getText();
app.add(appNumber);
app.add(name);
app.add(date);
app.add(fileLoc);
app.add(country);
}
in the database class i have the following code:
public class Database
{
public ArrayList<ArrayList> applicants;
/**
* Constructor for objects of class Database
*/
public Database()
{
applicants = new ArrayList<ArrayList>();
}
/**
* adds a new applicant to the database
*/
public void addApplicant(ArrayList app)
{
applicants.add(app);
}
public void list()
{
int n = applicants.size();
for(int i = 0; i < n ; i++)
System.out.println(applicants.get(i));
}
}
However when i add a user from the addUser class it does not appear when i use the list method in the Database class.
Any help would be appreciated.
Thanks.
I think you should try something like this to store data . It will be easy to insert and retrive .