Fill text field with return from another window

Asked

Viewed 1,153 times

1

I have a JTextField instantiated in the class time relay. By clicking on that JTextField, he calls another JFrame search.
When performing a search on this new JFrame and click on Confirm, I need him to fill in the JTextField first frame.

I tried in some ways, leaving the jtextfields public and with the following code:

relatorio.txt_id.setText(txt_id.getText());

I tried to create a method in the first Jframe where the Jtextfield is that receives the data by parameter

None of the attempts I made filled Jtextfield, how do I do that?

Call from the search screen:

 private void txt_idMouseClicked(java.awt.event.MouseEvent evt) {                                    
    busca busca = new busca(null, rootPaneCheckingEnabled);
    busca.setVisible(true);
}   

Class search screen:

import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Restrictions;

public class busca extends javax.swing.JDialog {

    private List list_atendentes;

    public busca(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        listar_pessoas();
    }

 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        SessionFactory sf = new Configuration().configure().buildSessionFactory();
        Session session = sf.openSession();
        Criteria crit_pessoa = session.createCriteria(Pessoa.class);
        crit_pessoa.add(Restrictions.like("nome", "%"+txt_nome.getText()+"%"));
        list_atendentes = crit_pessoa.list();
        TableModelBusca tm = new TableModelBusca(list_atendentes);
        table.setModel(tm);
    }                        

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        rel_tempo relatorio = new rel_tempo(null, rootPaneCheckingEnabled);
        relatorio.txt_id.setText(txt_id.getText());
        relatorio.txt_nome.setText(txt_nome.getText());
        dispose();
    }                                        

     public void listar_pessoas() throws HibernateException {
        SessionFactory sf = new Configuration().configure().buildSessionFactory();
        Session session = sf.openSession();
        Criteria crit_motivo = session.createCriteria(Pessoa.class);
        list_atendentes = crit_motivo.list();
        TableModelBusca tm = new TableModelBusca(list_atendentes);
        table.setModel(tm);
    }

}
  • Using Jdialog, instead of another Jframe, your problems are reduced by 90%. Post the search screen call and search screen class, which I can even suggest such a change in code.

  • I edited, give a look @diegofm

1 answer

1


How you turned your second screen(Search) into JDialog, create an attribute on it that will save the text you want to return:

private String strId;
private String strNome;

To facilitate the return, create a method that returns an array of Strings, so it is possible to return the two values together. If you prefer, create get for each one and return them separately

private String[] retornarTextos(){
  String[] valores = {txt_id.getText(), txt_nome.getText()};
  return valores;
}

In the event of the button that will end the search screen activity, there is no need to instantiate the main screen again, just assign the value you want to return the variables we have just created:

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

    strId = txt_id.getText());
    strNome = txt_nome.getText();
    dispose();
}

On the main screen, when the search screen is checked, it should receive as parameters the very instance of its main screen (it will be the Owner) and true, so that the search screen is superimposed on the main screen, blocking it until the search is closed (modal feature). To recover the entered values, just call the method created on the search screen right after the setVisible:

private void txt_idMouseClicked(java.awt.event.MouseEvent evt) {
    //passe a instancia da tela principal(rel_tempo)
    busca busca = new busca(seuFrame, true);
    busca.setVisible(true);
    String[] textosDigitados = busca.retornarTextos();
    //O id foi o primeiro indice a ser preenchido na outra classe
    //por isso o indice 0
    txt_id.setText(textosDigitados[0]);

} 

If you prefer to use getters to return the values of the fields of the second screen, just call them directly on setText of the main screen fields.


References with examples of use of Jdialogs:

How to Make Dialogs/Oracle

Set which Jframes to focus on?

Browser other questions tagged

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