我有两个窗口:PatientWindow和CreateEditPatientWindow。 在PatientWindow中,我有从文本文件填充的JTable。 另外,在PatientWindow上,我有“更新”按钮,可打开CreateEditPatientWindow。 因此,我想用PatientWindow表格中的选定项目的数据填充CreateEditPatientWindow中的TextField。
这里我更新了按钮监听器。在这里,我成功地在表中打印了所选用户的正确用户名:
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);
}
}
}
});
因此在CreateEditPatientWindow中,TextFields之一是txtUsername。
lblUsername = new JLabel("Username");
txtUsername = new JTextField(20);
// txtUsername.setText(USERNAME VALUE FROM PatientWindow)
答案 0 :(得分:0)
要将值从一个类传递到另一个类,则可以使用模型类。
示例:
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;
}
}
现在您可以使用此Model类存储您的值,
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();`