Fill text field with return from another window

1

I have JTextField instantiated in class rel_tempo . When you click this JTextField , it calls another JFrame of searches.
When I perform a search on this new JFrame and click Confirm , I need to fill in the JTextField of the first Frame.

I tried in some ways, leaving 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 by data parameter

None of the attempts I made filled the JTextField, how do I do this?

Search screen call:

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

Search screen class:

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);
    }

}
    
asked by anonymous 11.07.2016 / 16:29

1 answer

1

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

private String strId;
private String strNome;

To facilitate return, create a method that returns a vector of Strings, so you can 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 close 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 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 stalled, it should receive the parameters of its main screen (it will be owner ) and true , so that the search screen overlaps the main screen, locking it until the search is closed (modal feature). To retrieve the typed values, simply call the method created on the Search Screen just after 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, simply call them directly in the setText of fields on the main screen.

References with examples of using JDialogs:

How to Make Dialogs / Oracle

Defining which JFrames will be focused?

    
12.07.2016 / 16:45