1

Im creating a little patients management software to my wife. The program is fully functional but Im having a problem once I update patient data or exclude any register. I have a search form to bring all patients (using date period, name...) then I select desired patient (from last visit) and all patient's data are shown into another form. I can update data, exclude this visit but, once I save it, this form (secundary) is closed (dispose). But the main form (search form) is holding previous values. How can I do to refresh main form after closing secundary form??? thx a lot

Edited: Forgot to say its Java - sry ;)

Edited2: this is the method Im using to call secundary form. I used Netbeans to create the project.

private void btn_selecionaActionPerformed(java.awt.event.ActionEvent evt) {                                              
        try{
        int sel = tabela.getSelectedRow();

        if (sel != -1){
            String sql = "select * from agendados "
                    + "where numag = " + modelo.getValueAt(sel, 5);
            con_mnt.executaSQL(sql);
            func = new Funcoes();
            func.carregaDados(dados, con_mnt.rs);
            new CarregarAgendamento(func.getDados()).setVisible(true);

        } else{
            JOptionPane.showMessageDialog(null, "Selecione algum paciente antes.", "   Atenção!!!", JOptionPane.ERROR_MESSAGE);
        }

        }
        catch(SQLException | NumberFormatException e){
            JOptionPane.showMessageDialog(null, "Nao existe dados ainda", "   Atenção!!!", JOptionPane.ERROR_MESSAGE);
        }
    }

Edited 3: Save, Delete and salvarAgendamento methods:

private void btn_salvaActionPerformed(java.awt.event.ActionEvent evt) {                                          
    salvarAgendamento();
    dispose();
}                                         

private void btn_deleteActionPerformed(java.awt.event.ActionEvent evt) {                                           
    if(javax.swing.JOptionPane.showConfirmDialog(null,"Deseja realmente Excluir este Agendamento?","ATENÇÃO ",javax.swing.JOptionPane.YES_NO_OPTION )==0)
    {  
        con_ag = new Firebird(func.fullPath("/db/manutencao.fdb"));
        con_ag.removeFDB("agendados", "numag", jt_cod.getText());
        Agendados.refresh = 1;
        this.dispose();  
    }  

}

public void salvarAgendamento(){
        ArrayList<Object> colunas = new ArrayList<>();
        ArrayList<Object> valores = new ArrayList<>();
        calendario = new Calendario();

        if (jcb_motivo.getSelectedIndex() == -1)
        {
            JOptionPane.showMessageDialog(null, "Faltou o Motivo do Agendamento!");
            jcb_motivo.requestFocus();
        } 
        else if (jt_dataAg.getText().equals(""))
        {
            JOptionPane.showMessageDialog(null, "Faltou a Data do Agendamento!");
            jt_dataAg.requestFocus();
        } 
        else if (dados.getStatusAg() == 0)
        {
            JOptionPane.showMessageDialog(null, "Faltou selecionar o Status do Agendamento!");
            jcb_status.requestFocus();
        }
        else
        {
            calendario.dataFormatada("dd/mm/yyyy", "yyyy-mm-dd", jt_dataAg.getText());
            dados.setDataAg(calendario.getDataFormatada() + " 00:00:00");

            colunas.add("statusag");
            colunas.add("obs");

            valores.add(jt_tel1.getText());
            valores.add(jt_tel2.getText());
            valores.add(jt_cel.getText());
            valores.add(dados.getConvenioNum()); //convnum
            valores.add(dados.getDentistaNum()); //dentnum
            valores.add(jcb_motivo.getSelectedItem());
            valores.add(dados.getDataAg()); //dataag
            valores.add(dados.getStatusAg()); //statusag
            valores.add(area_obs.getText());
            valores.add(jt_cod.getText());

            grava(valores);
            JOptionPane.showMessageDialog(null, "Agendamento alterado com sucesso!");
            dispose();
        } 
    }
Galla
  • 61
  • 1
  • 7

1 Answers1

1

I would do it like this:

Assuming, you already have "close" button on second form.

1) I would send the first form to the second

 SecondForm second = new SecondForm(this);

Or

SecondForm second = new SecondForm(firstForm);

The init function of Second form would keep the firstForm instance and when closing, I would go something like that:

public void actionPerformed(ActionEvent e){
 firstForm.update();
 this.close();
}

Sorry for posting only small chunks of code, but the idea is:

  • Have stored instance of first form in your second form
  • When closing second form, via button or via "X" button top right, update first form via first's form public

EDIT I dont speak spannish (sorry if thats another language :) ), so I will do few assumptions: tabela is the component, which shows data. I am not that into JTable, but there is still update() function. Now how to do that. I would change the row

new CarregarAgendamento(func.getDados()).setVisible(true); 

to

new CarregarAgendamento(func.getDados(), this).setVisible(true);

Now, the this reffers to the first form class. Because I dont know how its called, I will call it FirstForm further on. ok?

So, the CarregarAgendamentois (another assumption) second form. I would update the init like this

public class CarregarAgendamento 
//all previous private field
private FirstForm first;

/* Here I assume that the func.getDados() returns Funcoes. If not, change it */
public CarregarAgendamento(Funcoes func, FirstForm f){
 //leave everything as it was, just add the line below
 this.first = f;
}

now the functions:

private void btn_salvaActionPerformed(java.awt.event.ActionEvent evt) {                                           
    salvarAgendamento(); 
    first.getTabela().update(); //method to update the table. 
    dispose(); 
}                                          

private void btn_deleteActionPerformed(java.awt.event.ActionEvent evt) {                                            
    if(javax.swing.JOptionPane.showConfirmDialog(null,"Deseja realmente Excluir este Agendamento?","ATENÇÃO ",javax.swing.JOptionPane.YES_NO_OPTION )==0) 
    {   
        con_ag = new Firebird(func.fullPath("/db/manutencao.fdb")); 
        con_ag.removeFDB("agendados", "numag", jt_cod.getText()); 
        Agendados.refresh = 1; 
        first.getTabela().update(); //method to update the table. 
        this.dispose();   
    }   

} 

public void salvarAgendamento(){ 
        ArrayList<Object> colunas = new ArrayList<>(); 
        ArrayList<Object> valores = new ArrayList<>(); 
        calendario = new Calendario(); 

        if (jcb_motivo.getSelectedIndex() == -1) 
        { 
            JOptionPane.showMessageDialog(null, "Faltou o Motivo do Agendamento!"); 
            jcb_motivo.requestFocus(); 
        }  
        else if (jt_dataAg.getText().equals("")) 
        { 
            JOptionPane.showMessageDialog(null, "Faltou a Data do Agendamento!"); 
            jt_dataAg.requestFocus(); 
        }  
        else if (dados.getStatusAg() == 0) 
        { 
            JOptionPane.showMessageDialog(null, "Faltou selecionar o Status do Agendamento!"); 
            jcb_status.requestFocus(); 
        } 
        else 
        { 
            calendario.dataFormatada("dd/mm/yyyy", "yyyy-mm-dd", jt_dataAg.getText()); 
            dados.setDataAg(calendario.getDataFormatada() + " 00:00:00"); 

            colunas.add("statusag"); 
            colunas.add("obs"); 

            valores.add(jt_tel1.getText()); 
            valores.add(jt_tel2.getText()); 
            valores.add(jt_cel.getText()); 
            valores.add(dados.getConvenioNum()); //convnum 
            valores.add(dados.getDentistaNum()); //dentnum 
            valores.add(jcb_motivo.getSelectedItem()); 
            valores.add(dados.getDataAg()); //dataag 
            valores.add(dados.getStatusAg()); //statusag 
            valores.add(area_obs.getText()); 
            valores.add(jt_cod.getText()); 

            grava(valores); 
            first.getTabela().update(); //method to update the table. 
            JOptionPane.showMessageDialog(null, "Agendamento alterado com sucesso!"); 
            dispose(); 
        }  
    } 

As stated before - I never worked with JTable, so I do not know exactly how to update it. Just hoping, that it will work. Obviously you have to add this function to your FirstForm somewhere:

  public JTable getTabela(){
     return tabela;
  }

If you dont have it already

Pavel Janicek
  • 14,128
  • 14
  • 53
  • 77
  • I added the method to load second form. Can you check it plz? thx – Galla Mar 18 '12 at 14:15
  • i just need refresh main form when i hit save button or delete button. Then it will apply changes or delete register and close this form. Im adding save and delete actions method. – Galla Mar 18 '12 at 17:15
  • thx a lot Pavel,,, I wasn't adding First form to Second form constructor. Now its working great. Thx a lot ;) – Galla Mar 18 '12 at 19:51