How to make a JFrame return an instance of a given object?

1

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.

    
asked by anonymous 29.06.2016 / 23:41

1 answer

1

When you call JFrameCadastroPessoa frame = new JFrameCadastroPessoa(); , the Person object is created with null attributes in the JFrame class, and the JRE follows normal flow, running all its main lines without interruption, and when it arrives at System.out.println(pessoa.toString()); , it displays the object created in its JFrameCadastroPessoa class, with null properties.

You should add the view inside the actionperformed so that it is displayed just by clicking the button, like this:

private void jBSalvarActionPerformed(java.awt.event.ActionEvent evt) {

        pessoa.setNome(jTFNome.getText());  
        pessoa.setApelido(jTFApelido.getSelectedText());
        System.out.println(pessoa.toString());
}

Here, you will see exactly what you type in the fields.

Update

After the comments, I created an example where you can use a custom JOptionPane, passing a JPanel to it:

public class ExibePessoa {
    public static void main(String[] args) {

        Pessoa pessoa;

        JPanel p1 = new JPanel();
        JPanel p2 = new JPanel();
        JPanel panelPrincipal = new JPanel(new BorderLayout());
        JLabel lbNome = new JLabel("Nome:");
        JTextField txtNome = new JTextField(20);
        JLabel lbApelido = new JLabel("Apelido:");
        JTextField txtApelido = new JTextField(20);
        p1.add(lbNome);
        p1.add(txtNome);
        p2.add(lbApelido);
        p2.add(txtApelido);
        panelPrincipal.add(p1, BorderLayout.NORTH);
        panelPrincipal.add(p2, BorderLayout.SOUTH);
        JOptionPane.showMessageDialog(null, panelPrincipal, "Acesso Restrito", JOptionPane.PLAIN_MESSAGE);
        pessoa = new Pessoa(txtNome.getText(), txtApelido.getText());

        System.out.println(pessoa.toString());
    }
}
    
30.06.2016 / 00:02