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

Asked

Viewed 1,315 times

1

I have a JFrame called Jframecadastre person which has only two fields name and surname, as shown in the image :

inserir a descrição da imagem aqui

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(); therefore Pessoa pessoa = frame.salvar();;

  • @diegofm Thanks for your attention ,but I’ve done it!! It’s the same! What would be EDT?

  • There is another problem in your code, you are not starting within the EDT.

1 answer

1


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

You must add the display inside the actionperformed so that it is displayed only by clicking the button, this way:

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

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

Here yes, it will be displayed exactly what is typed 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());
    }
}
  • I really wanted to return an object of the Person type from that Frame, its explanation this inexoravelemnte correct, esoteric aware of the above code! But this code of my question is part of a larger context in a project where I use Socktes and I need to return that same person object!!

  • @Penada you want to move to another frame? If yes, just make the other screen receive a person type object and pass to the other screen inside the actionPerformed, something like that: Frame2 = new JFrame(pessoa); and on the other screen you can receive normally.

  • I don’t want to move to another frame but I need the person object in the Exibepessoa class, I put System.out.println just to illustrate! If you want I can put the code on the github!! It seems like a joke at this point in the championship that I don’t know how to return this object in our class!!

  • @Penada painted this call comes from another canvas or this registration screen is the only one of your project?

  • This screen is really Unica. I’ve compiled this example to simplify the question!! Pos usually when we deal with frames we use MVC and we call facades and DAOS and everything follows wonderfully , but this example left in a dead end!! The whole structure of the project was placed here! The other larger project I abstract, because it would solve the problem there if it solved this our impasse in question!!

  • 1

    @Penada pintada como Voce disse na pergunta, o problema é que o Joptionpane funciona de forma que, while open, ele "trava" o restante da execução, diferente do Jframe, que continua executando após renderizar a tela.

  • That’s right, in this mentioned solution it hangs and works (using Joptionpane.showMessageDialog ), but when I try to do the same using Jframe no!! What I must confess left me without soil under my feet!!

  • @Penapintada you know you can create a custom Joptionpane, with this face of the jframe? Maybe it is better option.

  • Exactly , the problem is that when I was looking for my examples of custom Joptionpane I came across tons of projects and at the moment it is difficult to find , I do know that it is possible to customize , but at the moment, I’ll be honest I have no idea how to customize it, I completely forgot!! If you know, please post the code, and yours will be the final answer!! Lately I have been working only with projects with web interface!! I stopped practicing SWING, unfortunately!!

  • 1

    @Penapintada looks at the update. Remembering that you can configure the textfield size by changing the 20, I did here and got a little misaligned kkk

  • 1

    Very good guy! I updated my project here with the code and it worked, so I encapsulated your solution with using Panel in a class called Panelcadastro that has two methods: create Panelcadastro() and comeback Thanks!!

Show 6 more comments

Browser other questions tagged

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