1
I have a JFrame
called Jframecadastre person which has only two fields name and surname, as shown in the image :
Jframecadastre person:
public class JFrameCadastroPessoa extends javax.swing.JFrame {
private Pessoa pessoa = new Pessoa();
//codigos gerados automaticamente
//O código do botão salvar é o seguinte:
private void jBSalvarActionPerformed(java.awt.event.ActionEvent evt) {
pessoa.setNome(jTFNome.getText());
pessoa.setApelido(jTFApelido.getSelectedText());
}
private void jBLimparActionPerformed(java.awt.event.ActionEvent evt) {
jTFNome.setText("");
jTFApelido.setText("");
}
public Pessoa salvar() {
return pessoa;
}
}
Classe Pessoa
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";
}
}
Person in need
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 he always displays the result as :
{ nome=null, apelido=null}
I would like to display the data I entered in the frame JFrameCadastroPessoa
.
What should I do to return this object in the Exibepessoa class?
The problem is that when I run the Exibepessoa class on the console it is already printing { name=null, nickname=null} on the output before I even enter the data into the frame!!!
Using the JOptionPane.showInputDialog
in the class Exibepessoa I get the expected balance but I really wanted to use was the 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 resolve things simply within the class JFrameCadastroPessoa
and solve everything in the way private void jBSalvarActionPerformed(java.awt.event.ActionEvent evt)
, but really need the solution so that the result comes from the save() method that returns a Person.
Replace that
Pessoa pessoa = new Pessoa();
thereforePessoa pessoa = frame.salvar();;
– user28595
@diegofm Thanks for your attention ,but I’ve done it!! It’s the same! What would be EDT?
– Pena Pintada
There is another problem in your code, you are not starting within the EDT.
– user28595