I have a JFrame
called JFrameCadastroPessoa that has only two fields first and last name, as the image shows:
JFrameCadastroPeople:
publicclassJFrameCadastroPessoaextendsjavax.swing.JFrame{privatePessoapessoa=newPessoa();//codigosgeradosautomaticamente//Ocódigodobotãosalvaréoseguinte:privatevoidjBSalvarActionPerformed(java.awt.event.ActionEventevt){pessoa.setNome(jTFNome.getText());pessoa.setApelido(jTFApelido.getSelectedText());}privatevoidjBLimparActionPerformed(java.awt.event.ActionEventevt){jTFNome.setText("");
jTFApelido.setText("");
}
public Pessoa salvar() {
return pessoa;
}
}
Person Class
public class Pessoa implements Serializable {
String nome;
String apelido;
public Pessoa() {
}
public Pessoa(String nome, String apelido) {
this.nome = nome;
this.apelido = apelido;
}
//getters and setters
public String toString() {
return " { nome=" + nome + ", apelido=" + apelido + '}' + "\n";
}
}
DisplayPerson
public class ExibePessoa {
public static void main(String[] args) {
JFrameCadastroPessoa frame = new JFrameCadastroPessoa();
frame.setVisible(true);
Pessoa pessoa = new Pessoa();
pessoa = frame.salvar();
System.out.println(pessoa.toString());
}
}
I want to know why it always displays the result as:
{ nome=null, apelido=null}
I would like to display the data I entered in the JFrameCadastroPessoa
frame.
What do I have to do to return this object in the PersonPreview class?
The problem is that when I run the ViewPeople class on the console it will immediately print {name = null, nickname = null} in the output before I even insert the data into the frame !!!
Using JOptionPane.showInputDialog
in the class DisplaysPeople I get the expected result but wanted to use even was JFrame
!!
I know this has a very obvious answer but I have tried everything, and it is my knowledge that it would be more plausible to solve things simply inside the JFrameCadastroPessoa
class and solve everything in the private void jBSalvarActionPerformed(java.awt.event.ActionEvent evt)
method, but I really need the solution so that the result comes from the save () method that returns a Person.