0

I have two windows: PatientWindow and CreateEditPatientWindow. In PatientWindow I have JTable which is populated from text files. Also, on the PatientWindow I have button 'Update' which open CreateEditPatientWindow. So I want to populate TextFields in CreateEditPatientWindow with data of selected item from table in PatientWindow.

Here i update button listener. Here i successed to print line the correct username of selected user in table:

btnUpdate.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int row = tblPatients.getSelectedRow();
                if(row == -1)
                {
                    JOptionPane.showMessageDialog(null, "You must select row for update", "Info", JOptionPane.WARNING_MESSAGE);
                }
                else 
                {
                    DefaultTableModel model = (DefaultTableModel)tblPatients.getModel();
                    String username = model.getValueAt(row, 6).toString();
                    UserModel userSearch = UsersClass.findUser(username);                   


                    if(userSearch != null)
                    {

                        System.out.println("USER FOUND!!!" + username);


                        CreateEditPatientWindow createEditPatientWindow = new CreateEditPatientWindow();
                        createEditPatientWindow.setVisible(true);
                    }
                    else
                    {
                        JOptionPane.showMessageDialog(null, "User not found", "Info", JOptionPane.ERROR_MESSAGE);
                    }
                }
            }
        });

So in CreateEditPatientWindow one of TextFields is txtUsername.

lblUsername = new JLabel("Username");
txtUsername = new JTextField(20);
// txtUsername.setText(USERNAME VALUE FROM PatientWindow)
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Nitrus Brio
  • 89
  • 2
  • 15

1 Answers1

0

To pass values from one class to another then you can use Model Class.

Example:

InputPatientWindow.java

Class InputPatientWindow{
 private String userName;
 Private String lastName;
 private String namem;
 private String password;

 public void setUserName(String userName){
   this.userName = userName;
 }
 public String getUserName(){
  return userName;
 }

 public void setLastName(String lastName){
   this.lastName= lastName;
 }
 public String getLastName(){
  return lastName;
 }

 public void setNamem(String namem){
   this.namem= namem;
 }
 public String getNamem(){
  return namem;
 }

 public void setPassword(String password){
   this.password= password;
 }
 public String getPassword(){
  return password;
 }
}

Now you can use this Model class to store your values,

PatientWindow

btnUpdate.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int row = tblPatients.getSelectedRow();
                if(row == -1)
                {
                    JOptionPane.showMessageDialog(null, "You must select row for update", "Info", JOptionPane.WARNING_MESSAGE);
                }
                else 
                {
                    DefaultTableModel model = (DefaultTableModel)tblPatients.getModel();
                    String username = model.getValueAt(row, 6).toString();
                    UserModel userSearch = UsersClass.findUser(username);                   


                    if(userSearch != null)
                    {

                        System.out.println("USER FOUND!!!" + username);
                        InputPatientWindow ipw = new InputPatientWindow();
                        ipw.setUserName(username);
                        /*In same way set all further parameters you want in Create Edit PatientWindow*/
                        /*pass the data through constructor*/
                        CreateEditPatientWindow createEditPatientWindow = new CreateEditPatientWindow(ipw);


                        createEditPatientWindow.setVisible(true);
                    }
                    else
                    {
                        JOptionPane.showMessageDialog(null, "User not found", "Info", JOptionPane.ERROR_MESSAGE);
                    }
                }
            }
        });

CreateEditPatientWindow.java

/*Write your code and add the constructor*/

public InputPatientWindow  ipw = new InputPatientWindow();

CreateEditPatientWindow(InputPatientWindow ipw){
 this.ipw = ipw;
}

//And now you can access your values from ipw by using below methods.
`ipw.getUserName();`
piet.t
  • 11,718
  • 21
  • 43
  • 52
Dushyant Tankariya
  • 1,432
  • 3
  • 11
  • 17