Calling an Arraylist from one Jframe to another

Asked

Viewed 1,025 times

2

I need to call a ArrayList raised in a JFrame to another JFrame. In this case, the user would make a registration in one of the Jframes, called Registration, 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 puts his 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 registrar, which are allocated in Arraylist. That’s why I need the Arraylist created in the file "Register" in the file "Login".

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 action.

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

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

  • Please avoid long discussions in the comments; your talk was moved to the chat

  • Well, to start a conversation in a chat I need to have X reputation points, so the long discussion..

  • The message on my behalf above is automatically generated after I move the comments to the chat, but still it doesn’t ask you to have long discussions in the chat, it says to avoid long discussions, anywhere, and the ones that were here were moved to the chat. The ideal is that all the information needed to properly understand your question is present in the body of the question and not in the chat or comments.

1 answer

0


In the call of your 2nd frame, pass as parameter the array of users you already have.

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

and then to use the list in the second frame, something similar to that

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’ve helped.

  • It helped a lot, thank you very much!!!

  • I already had the same doubt when I did Chat project with Socket in Java. ...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.