Calling an ArrayList from one JFrame to another

2

I need to call a ArrayList created in JFrame to another JFrame . In this case, the user would make a record in one of the JFrames, called Cadastre, and from this would create the User object, with login and password and this object will be allocated in an ArrayList. From this, the user goes to the login area and places their login and password, in another JFrame, called login.

In this new JFrame, the program needs to check if the login and password match any of the logins and passwords of the objects created by the registry, which are allocated in the ArrayList. So I need the ArrayList created in the "Register" file in the "Login" file.

The code below, in the first JFrame.

ArrayList<Usuario> lista = new ArrayList<>();

public ArrayList<Usuario> getLista() {
    return lista;
}

public void setLista(ArrayList<Usuario> lista) {
    this.lista = lista;
}

In the code below, in the second JFrame, where I need this ArrayList.

    Usuario p;
    List novalista = new Cadastro().getLista();
    p = (Usuario) novalista.get(i); 

To call the JFrame files, I use the push button.

          Cadastro frame = new Cadastro(); 
          frame.setVisible(true); 
          this.dispose();

In the Login file (where I need to do the validations) I have already tried to pull in several ways, but none works.

    
asked by anonymous 18.10.2015 / 04:20

1 answer

0

When calling your 2nd frame, pass the array of users you already have as a parameter.

 Cadastro frame = new Cadastro(getLista()); //a lista do 1º frame
 frame.setVisible(true); 
 this.dispose();

and hence to use the list in the second frame, something like this

private List<Usuario> usuario;
public Cadastro(List<Usuario> usuario){

         //inicia os componentes
         // ....

        //utiliza o seu parametro
        this.usuario = usuario;

         //segue a logica
}

I hope I have helped.

    
19.10.2015 / 17:03