0

When I open my AdminPanel and it contains the JTable with PatientRecords and button to open the frame to Add Patient.. after filling up the form I click the ADD button and it will save to the database

int YesOrNo = JOptionPane.showConfirmDialog(null, "Do you want to save?", "Save",JOptionPane.YES_NO_OPTION);
    if(YesOrNo == 0){
        try{
        String sql = "Insert into Patient_Records (First_Name,Last_Name,MI,Age,Birth_Date,Gender,Marital_Status,Street,City,[State/Province],Zip_Code,Country,Contact_No,Email_Address,Registered_Date,[In/Out]) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        pst = conn.prepareStatement(sql);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String now = (sdf.format(new java.util.Date()));
                pst.setString(1, ADDFIRSTNAME.getText());
                pst.setString(2, ADDLASTNAME.getText());
                pst.setString(3, ADDMIDDLEINITIAL.getText());
                pst.setString(4, ADDAGE.getText());
                pst.setString(5, ((JTextField)ADDDATE.getDateEditor().getUiComponent()).getText());
                pst.setString(6, (String) ADDGENDERBOX.getSelectedItem());
                pst.setString(7, (String)ADDSTATUS.getSelectedItem());
                pst.setString(8, ADDADDRESS.getText());
                pst.setString(9, ADDADDRESS1.getText());
                pst.setString(10, ADDADDRESS2.getText());
                pst.setString(11, jTextField3.getText());
                pst.setString(12, (String)ADDCOUNTRY.getSelectedItem());
                pst.setString(13, jTextField1.getText());
                pst.setString(14, jTextField2.getText());
                pst.setString(15, now);
                pst.setString(16, "0");
        String CN = jTextField1.getText();
        String EA = jTextField2.getText();
        String F = ADDFIRSTNAME.getText();
        String M = ADDLASTNAME.getText();
        String L = ADDMIDDLEINITIAL.getText();
        if(!ConValid(CN)){
            JOptionPane.showMessageDialog(null, "Contact Number is not valid","Invalid Input",JOptionPane.WARNING_MESSAGE);
        }else if(!ConValid(CN)){
            JOptionPane.showMessageDialog(null, "Contact Number must contain 11 number(s)","Invalid Input",JOptionPane.WARNING_MESSAGE);
        }else if(!valEmail(EA)){
            JOptionPane.showMessageDialog(null, "Invalid Email Address","Invalid Input",JOptionPane.WARNING_MESSAGE);
        }else if(!OnLetter(F) || !OnLetter(M) || !OnLetter(L)){
            JOptionPane.showMessageDialog(null, "Name must not contain number(s)","Invalid Input",JOptionPane.WARNING_MESSAGE);
        }else{
            JOptionPane.showMessageDialog(null, "Saved","Saved",JOptionPane.INFORMATION_MESSAGE);
            pst.execute();
            AdminPortal AP = new AdminPortal();
            AP.PatientRec();
        }
    }catch(Exception e){
        JOptionPane.showMessageDialog(null, e);
    }
    }else{

    }

after that I exit the Frame that I fill with Patient records and the AdminPanel with JTable that contains PatientRecords did mot Refresh

I tried this code

 AdminPortal AP = new AdminPortal();
                AP.PatientRec();

what should I do?

Prashant Gupta
  • 788
  • 8
  • 26
Sieccc
  • 49
  • 6
  • Where do you change the data backing your `JTable`? – Izruo Oct 05 '18 at 05:44
  • Did you try searching little bit? I found this for you: [How to refresh JTable after inserting data to database?](https://stackoverflow.com/questions/27302445/how-to-refresh-jtable-after-inserting-data-to-database) – prasad_ Oct 05 '18 at 05:47
  • Start by taking a look at [How to Make Dialogs](https://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html). You question is too broad and is missing important details which we'd need to provide any reasonable answer without having to make guesses – MadProgrammer Oct 05 '18 at 06:30
  • Welcome to SO. There is no issue description in your question. Furthermore, there is actually no question. As part of the review process, it is recommended voting to close this question. Please read this links before posting a new question on SO: [How to Ask](https://stackoverflow.com/help/how-to-ask) and [How to write a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). – sɐunıɔןɐqɐp Oct 05 '18 at 06:40

1 Answers1

0

Here table needs to be refreshed with latest data. To do this follow below steps.

  • Clear the JTable instance after save operation completes successfully.
  • Invoke routine/ method used to display PatientRecords, fresh invocation will make sure latest data is reflected in JTable instance.

In terms of code:

else{
            JOptionPane.showMessageDialog(null, "Saved","Saved",JOptionPane.INFORMATION_MESSAGE);
            pst.execute();

            //invoke method to clear/ reset JTable.

            //invoke method to populate JTable with fresh data.

            AdminPortal AP = new AdminPortal();
            AP.PatientRec();
        }
Dark Knight
  • 8,218
  • 4
  • 39
  • 58
  • There's no need to purge and re-populate all data. You can register a [`TableModelListener`](https://docs.oracle.com/javase/7/docs/api/javax/swing/event/TableModelListener.html) on the backing [`TableModel`](https://docs.oracle.com/javase/7/docs/api/javax/swing/table/TableModel.html) and notify it of the exact change(s). – Izruo Oct 05 '18 at 06:00
  • @Izruo thanks for suggestion. I think there should be some trigger point to refresh data. It can be anything – Dark Knight Oct 05 '18 at 06:03