-1

Hey I am creating a java project. In which I have a insert record frame, on insert frame I have a option to enter father ID and if the user did not know the father id, so I have set a button to find the father id. when the user will click on that button, the new frame will appear and user can search for id, the result will show in a table and when user click on the particular record, the frame will dispose and should set the respective id on the previous frame.

I have written the code for it, and it is passing the value to the previous frame but it is not setting the value to the textfield that I want it to be. Where I am doing wrong? Here is the code.

FamilyInsert.java

public class FamilyInsert extends javax.swing.JFrame {

/**
 * Creates new form FamilyInsert
 */

int id = DBManager.genID();
public int fid;
public FamilyInsert() {
    initComponents();
    txtId.setText(""+id);
    txtName.requestFocus();
}



public void setFid(int fid){
    txtFid.setText(""+fid);
    System.out.println("setFID "+fid);
}

public void reset()
{
    txtName.setText("");
    txtFather.setText("");
    txtFid.setText("");
    txtCity.setText("");
    txtState.setText("");
    txtName.requestFocus();

}


private void btnSubmitActionPerformed(java.awt.event.ActionEvent evt) {                                          
   int id = Integer.parseInt(txtId.getText());
   String name = txtName.getText();
   String fname = txtFather.getText();
   int fid = Integer.parseInt(txtFid.getText());
   String city = txtCity.getText();
   String state = txtState.getText();
   Family family = new Family(id,name,fname,fid, city,state);
    boolean flag = false;

    flag = DBManager.insertMember(family);
    if(flag==true){
        JOptionPane.showMessageDialog(this,"Successfully Saved");
        id++;
        txtId.setText(""+id);
        reset();
    }
    else
    {
        JOptionPane.showMessageDialog(this,"Error Occured");
    }



}                                         

private void txtFidActionPerformed(java.awt.event.ActionEvent evt) {                                       
    // TODO add your handling code here:
}                                      

private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {                                          
  SearchFatherFrame f = new SearchFatherFrame();
  f.setLocationRelativeTo(null);
  f.setVisible(true);

}                                         

}

and from the search frame:

 private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {                                     
       int id;
        if(evt.getClickCount()==2){
            if(jTable1.getSelectedRow()!=-1)
            {
                int index = jTable1.getSelectedRow();
                Family s = list.get(index);
                id = s.getId();
                System.out.println("ID from search frame "+id);
                FamilyInsert f = new FamilyInsert();
                f.setFid(id);
                this.dispose();
                //JOptionPane.showMessageDialog(this, s.getId()+"\n"+s.getName());                    
            }
        }
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • is `FamilyInsert` the class which holds the GUI?? – XtremeBaumer Mar 15 '18 at 13:55
  • 2
    Common newbie problem here: This is wrong `new FamilyInsert();`. Don't want to create a *new* FamilyInsert object but rather set the text of the already existing object. You need to understand what objects/instances are and what references are, and then use that knowledge to help you pass the correct reference to where it is needed. – Hovercraft Full Of Eels Mar 15 '18 at 13:56
  • Yes.. It is the Insert frame, where user has to insert all the record and the search frame is the second frame which should return the value and then dispose. – Pankaj Chaudhary Mar 15 '18 at 13:56
  • Also, no you don't want to use multiple JFrames. Please see: [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/questions/9554636) – Hovercraft Full Of Eels Mar 15 '18 at 14:00

2 Answers2

1

Your problem is that you're creating a new FamilyInsert object within the other class, and changing its state, but this leaves the state of the original FamilyInsert object unchanged. What you need to do instead is to pass a reference of the original displayed FamilyInsert into the 2nd object, and then change its state.

Change this:

SearchFatherFrame f = new SearchFatherFrame();

to something more like:

SearchFatherFrame f = new SearchFatherFrame(this);

Pass the reference into the class and use to set a field:

public class SearchFatherFrame {
    private FamilyInsert familyInsert;

    public SearchFatherFrame(FamilyInsert familyInsert) {
        this.familyInsert = familyInsert;
        // other code....
    }
}

Then use that reference passed in to change the state of the original object.

if(jTable1.getSelectedRow()!=-1) {
    int index = jTable1.getSelectedRow();
    Family s = list.get(index);
    id = s.getId();
    System.out.println("ID from search frame "+id);

    // FamilyInsert f = new FamilyInsert();
    // f.setFid(id);
    familyInsert.setFid(id); // **** add

    this.dispose();
    //JOptionPane.showMessageDialog(this, s.getId()+"\n"+s.getName());                    
}

Also you want the 2nd window to be a JDialog not a JFrame. Please see: The Use of Multiple JFrames, Good/Bad Practice?

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Wow! Thank you so much.. It works.. Sure, I will definitely avoid using Multiple JFrames. – Pankaj Chaudhary Mar 15 '18 at 14:12
  • @PankajChaudhary: much more important is do you understand the conceptual issue at play here and why your original code did not work? Objects are self-contained entities, and changing the state of one (your new FamilyInsert object) will not magically affect the state of another (the original and displayed FamilyInsert object). This is a key concept that you have to know inside and out to move forward. Please ask if any questions. – Hovercraft Full Of Eels Mar 15 '18 at 14:14
-2

Could you try

public void setFid(int fid){
        txtFid.setText(""+fid);
        System.out.println("setFID "+fid);
        yourJFrame.setVisible(true); //Reloads the frame
}
IX33U
  • 85
  • 4