我用JTable创建了一个窗口,其中包含有关某些用户的文本文件中的值。 另外,还有用于创建,编辑和删除用户的按钮。 我想使用并打开相同的“创建和编辑”按钮窗口,但是如果单击“编辑”按钮,则应使用JTable中的高亮(单击)用户的值填充窗口。
//PatientWindow.java
private void initActions() {
btnCreate.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
CreateEditPatientWindow createEditPatientWindow = new CreateEditPatientWindow();
createEditPatientWindow.setVisible(true);
}
});
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);
//PatientModel patientModel = patient;
//patientModel.setUsername(username);
CreateEditPatientWindow createEditPatientWindow = new CreateEditPatientWindow(USE VALUES OF SELECTED USER);
createEditPatientWindow.setVisible(true);
}
else
{
JOptionPane.showMessageDialog(null, "User not found", "Info", JOptionPane.ERROR_MESSAGE);
}
}
}
});
}
private void initGUI() {
tbToolBar = new JToolBar();
btnCreate = new JButton("Create");
btnUpdate = new JButton("Update");
btnDelete = new JButton("Delete");
tbToolBar.add(btnCreate);
tbToolBar.add(btnUpdate);
tbToolBar.add(btnDelete);
spScroll = new JScrollPane();
ArrayList<PatientModel> patientsList = UsersClass.getPatients();
String[] header = new String[] {
"Name",
"Lastname",
"UMCN",
"Gender",
"Adress",
"Phone number",
"Username",
"Password",
//"User type",
"Insurance number",
"Insurance category",
"Expiry date"};
Object[][] body = new Object[patientsList.size()][header.length];
int a = 0;
for(int i = 0; i < patientsList.size(); i++)
{
PatientModel patientsData = patientsList.get(i);
body[i][0] = patientsData.getName();
body[i][1] = patientsData.getLastname();
body[i][2] = patientsData.getUmcn();
body[i][3] = patientsData.getGender();
body[i][4] = patientsData.getAdress();
body[i][5] = patientsData.getPhoneNumber();
body[i][6] = patientsData.getUsername();
body[i][7] = patientsData.getPassword();
//body[i][8] = patientsData.getUserType();
body[i][8] = patientsData.getHealthInsuranceNumber();
body[i][9] = patientsData.getInsuranceCategory();
body[i][10] = patientsData.getExpiryDate();
}
DefaultTableModel tableModel = new DefaultTableModel(body,header);
tblPatients = new JTable(tableModel);
//tblPatients.setRowSelectionAllowed(true);
//tblPatients.setColumnSelectionAllowed(true);
//tblPatients.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
tblPatients.setDefaultEditor(Object.class, null);
JScrollPane tableScroll = new JScrollPane(tblPatients);
add(spScroll);
add(tbToolBar, BorderLayout.NORTH);
add(tableScroll, BorderLayout.CENTER);
}
//CreateEditPatientWindow.java
private void initGUI() {
MigLayout mig = new MigLayout("wrap 2", "[][]", "[]10[][]10[]");
setLayout(mig);
lblName = new JLabel("Name");
txtName = new JTextField(20);
lblLastname = new JLabel("Lastname");
txtLastname = new JTextField(20);
lblUMCN = new JLabel("Unique Master Citizen Number");
txtUMCN = new JTextField(20);
lblGender = new JLabel("Gender");
cbGender = new JComboBox();
cbGender.addItem("Male");
cbGender.addItem("Female");
lblAdress = new JLabel("Adress");
txtAdress = new JTextField(20);
lblPhoneNumber = new JLabel("Phone number");
txtPhoneNumber = new JTextField(20);
lblUsername = new JLabel("Username");
txtUsername = new JTextField(20);
lblPassword = new JLabel("Password");
txtPassword = new JTextField(20);
lblHealthInsuranceNumber = new JLabel("Health insurance number");
txtHealthInsuranceNumber = new JTextField(20);
lblExpiryDate = new JLabel("Expiry date");
txtExpiryDate = new JTextField(20);
lblInsuranceCategory = new JLabel("Insurance category");
cbInsuranceCategory = new JComboBox();
cbInsuranceCategory.addItem("1");
cbInsuranceCategory.addItem("2");
cbInsuranceCategory.addItem("3");
btnConfirm = new JButton("Confirm");
add(lblName);
add(txtName);
add(lblLastname);
add(txtLastname);
add(lblUMCN);
add(txtUMCN);
add(lblGender);
add(cbGender);
add(lblAdress);
add(txtAdress);
add(lblPhoneNumber);
add(txtPhoneNumber);
add(lblUsername);
add(txtUsername);
add(lblPassword);
add(txtPassword);
add(lblHealthInsuranceNumber);
add(txtHealthInsuranceNumber);
add(lblExpiryDate);
add(txtExpiryDate);
add(lblInsuranceCategory);
add(cbInsuranceCategory);
add(btnConfirm);
}
这里还有UsersClass,其中包含用于用各种类型的用户(患者,护士,医生等)填充Array的方法。
public class UsersClass {
public static ArrayList<UserModel> users = getUsers();
public static ArrayList<PatientModel> patients = getPatients();
public static ArrayList<UserModel> getUsers()
{
ArrayList<UserModel> usersList = new ArrayList<UserModel>();
ArrayList<PatientModel> patientsList = getPatients();
ArrayList<NurseModel> nursesList = getNurses();
ArrayList<DoctorModel> doctorsList = getDoctors();
usersList.addAll(patientsList);
usersList.addAll(nursesList);
usersList.addAll(doctorsList);
return usersList;
}
public static ArrayList<PatientModel> getPatients()
{
ArrayList<PatientModel> patientsList = new ArrayList<PatientModel>();
try
{
File patientTextFile = new File("src/txt/patients");
BufferedReader br = new BufferedReader(new FileReader(patientTextFile));
String line = null;
while((line = br.readLine()) != null)
{
String[] split = line.split("\\|");
String name = split[0];
String lastname = split[1];
String umcn = split[2];
String gender = split[3];
String adress = split[4];
String phoneNumber = split[5];
String username = split[6];
String password = split[7];
String userType = split[8];
int healthInsuranceNumber = Integer.parseInt(split[9]);
String expiryDate = split[10];
int insuranceCategory = Integer.parseInt(split[11]);
PatientModel newPatient = new PatientModel(
name,
lastname,
umcn,
gender,
adress,
phoneNumber,
username,
password,
userType,
healthInsuranceNumber,
expiryDate,
insuranceCategory);
patientsList.add(newPatient);
}
}
catch (Exception e)
{
e.printStackTrace();
}
return patientsList;
}
public static ArrayList<NurseModel> getNurses()
{
ArrayList<NurseModel> nursesList = new ArrayList<NurseModel>();
try
{
File nurseTextFile = new File("src/txt/nurses");
BufferedReader br = new BufferedReader(new FileReader(nurseTextFile));
String line = null;
while((line = br.readLine()) != null)
{
String[] split = line.split("\\|");
String name = split[0];
String lastname = split[1];
String umcn = split[2];
String gender = split[3];
String adress = split[4];
String phoneNumber = split[5];
String username = split[6];
String password = split[7];
String userType = split[8];
double salary = Double.parseDouble(split[9]);
String service = split[10];
NurseModel newNurse = new NurseModel(
name,
lastname,
umcn,
gender,
adress,
phoneNumber,
username,
password,
userType,
salary,
service);
nursesList.add(newNurse);
}
}
catch (Exception e)
{
e.printStackTrace();
}
return nursesList;
}
public static ArrayList<DoctorModel> getDoctors()
{
ArrayList<DoctorModel> doctorsList = new ArrayList<DoctorModel>();
try
{
File doctorTextFile = new File("src/txt/doctors");
BufferedReader br = new BufferedReader(new FileReader(doctorTextFile));
String line = null;
while((line = br.readLine()) != null)
{
String[] split = line.split("\\|");
String name = split[0];
String lastname = split[1];
String umcn = split[2];
String gender = split[3];
String adress = split[4];
String phoneNumber = split[5];
String username = split[6];
String password = split[7];
String userType = split[8];
double salary = Double.parseDouble(split[9]);
String specialization = split[10];
String service = split[11];
DoctorModel newDoctor = new DoctorModel(
name,
lastname,
umcn,
gender,
adress,
phoneNumber,
username,
password,
userType,
salary,
specialization,
service);
doctorsList.add(newDoctor);
}
}
catch (Exception e)
{
e.printStackTrace();
}
return doctorsList;
}
public static UserModel findUser(String username)
{
for(UserModel user : users)
{
if(user.getUsername().equals(username))
{
return user;
}
}
return null;
}
}
答案 0 :(得分:0)
我建议您:
为“用户”对象创建自定义TableModel。有关如何执行此操作的分步示例,请参见Row Table Model。
然后,当您创建弹出对话框时,将“用户”对象传递给对话框。如果单击“新建”按钮,则会传入一个空的“用户”对象。如果单击“编辑”按钮,则会从自定义TableModel中获得“用户”对象
对话框类中的代码,然后简单地显示“ User”对象中的数据。
然后,当对话框关闭时,您需要使用在对话框文本字段中输入的数据来更新“用户”对象。如果单击“编辑”,则只需重新绘制表以查看新数据。如果单击“新建”,则将用户对象添加到TableModel。