Access Different Jframes objects (JAVA)

Asked

Viewed 28 times

0

Good afternoon, Can someone help me with a project?

I’m developing a simple vaccine system,

but I’m having a hard time accessing attributes between a Jframe and another since in each Jframe I have to instantiate an object as new...

for example I have 2 jframes, I register the data of a user in one, and I need to rescue this data both to log in, and to access the user panel...

for example: Jframe1.java:

Cadastro cadastro = new cadastro();
cadastro.CadastrarUsuario(txtNome.getText(), txtSenha.getText());

then the register class will receive the data through the "register" instance, with the data that was passed to the "Register user" method using getNome() and getName in another Jframe, but how will I access this instance in another Jframe to rescue the same data? something like, Jframe2.java:

Cadastro cadastro;    
cadastro.Login(getNome(), getSenha());

if I create a new instance for the registration object in Jframe2 I cannot access the data originally entered in Jframe1...

If anyone would be willing to help me, I would appreciate it very much...

NOTE: I cannot use database in this application, so I need the data stored at runtime.

2 answers

0

Forgive me if I misunderstood your question, but to get attributes from an initial Jframe1 in another Jframe2, you must pass the current Jframe1 instance as a parameter in the Jframe2 constructor, claiming that Jframe1 has public methods that return data from your Jtextfield. Example:

Frame/Main Class:

public class Frame1 {        
   public String getTxtNome(){
      return this.txtNome.getText();
   }
   public String getTxtSenha(){
      return this.txtSenha.getText();
   }
   //Em qualquer lugar do seu código que chame o Frame2, que irá receber os dados:
   Frame2 frame2 = new Frame2(this);
}

Frame/Class that will receive the data:

public class Frame2 {

   private final Frame1 mainFrame;   

   public Frame2(Frame1 frame){
      this.mainFrame = frame;
   }

}

From this, you can access the methods created in Frame1 through the "mainframe" object created in Frame2.

0

Look. If I remember correctly, a Swing application can only have a Jframe, but more than one Jdialog, which can be instantiated to appear in front of Jframe but only after Jframe has already been created. The Jframe would be your main window, and if you want to fill out a registration you need to respect the order that is first instantiate the Jframe and then instantiate and make visible a registration Jdialog.

Note that when Jdialog appears, to not let the user return to Jframe without first closing Jdialog this must have been instantiated as modal.

This question in Soen tells how to pass data from one to the other. Basically you must build the API of your Jdialog class so you can ask it, from Jframe, the data that was filled by the user in Jdialog. Basically, create getters in Jdialog and call them from the Jframe.

Browser other questions tagged

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