简体   繁体   English

如何在另一个窗口中使用JTable中的选定值填充Textfields?

[英]How to populate Textfields with selected values from JTable on another window?

I have two windows: PatientWindow and CreateEditPatientWindow. 我有两个窗口:PatientWindow和CreateEditPatientWindow。 In PatientWindow I have JTable which is populated from text files. 在PatientWindow中,我有从文本文件填充的JTable。 Also, on the PatientWindow I have button 'Update' which open CreateEditPatientWindow. 此外,在PatientWindow上,我有一个按钮'Update',它打开CreateEditPatientWindow。 So I want to populate TextFields in CreateEditPatientWindow with data of selected item from table in PatientWindow. 所以我想在CreateEditPatientWindow中使用PatientWindow中表格中所选项目的数据填充TextFields。

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. 所以在CreateEditPatientWindow中,TextFields之一是txtUsername。

lblUsername = new JLabel("Username");
txtUsername = new JTextField(20);
// txtUsername.setText(USERNAME VALUE FROM PatientWindow)

To pass values from one class to another then you can use Model Class. 要将值从一个类传递到另一个类,可以使用Model Class。

Example: 例:

InputPatientWindow.java 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, 现在您可以使用此Model类来存储您的值,

PatientWindow 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 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();`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM